<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="表单验证" tabindex="-1"><a class="header-anchor" href="#表单验证"><span>表单验证</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#validation-quickstart">快速开始</a>
<ul>
<li><a href="#quick-defining-the-routes">定义路由</a></li>
<li><a href="#quick-creating-the-controller">创建控制器</a></li>
<li><a href="#quick-writing-the-validation-logic">编写验证逻辑</a></li>
<li><a href="#quick-displaying-the-validation-errors">显示验证错误信息</a></li>
<li><a href="#repopulating-forms">回填表单</a></li>
<li><a href="#a-note-on-optional-fields">可选字段的注意事项</a></li>
<li><a href="#validation-error-response-format">验证错误响应的格式化</a></li>
</ul>
</li>
<li><a href="#form-request-validation">表单请求验证</a>
<ul>
<li><a href="#creating-form-requests">创建表单请求类</a></li>
<li><a href="#authorizing-form-requests">表单请求授权验证</a></li>
<li><a href="#customizing-the-error-messages">自定义错误消息</a></li>
<li><a href="#preparing-input-for-validation">表单输入预处理</a></li>
</ul>
</li>
<li><a href="#manually-creating-validators">手动创建验证器</a>
<ul>
<li><a href="#automatic-redirection">自动重定向</a></li>
<li><a href="#named-error-bags">命名错误包</a></li>
<li><a href="#manual-customizing-the-error-messages">自定义错误消息</a></li>
<li><a href="#performing-additional-validation">验证后的钩子</a></li>
</ul>
</li>
<li><a href="#working-with-validated-input">使用验证后的表单输入</a></li>
<li><a href="#working-with-error-messages">使用验证错误信息</a>
<ul>
<li><a href="#specifying-custom-messages-in-language-files">在本地化文件中指定自定义消息</a></li>
<li><a href="#specifying-attribute-in-language-files">在本地化文件中指定属性</a></li>
<li><a href="#specifying-values-in-language-files">在本地化文件中指定值</a></li>
</ul>
</li>
<li><a href="#available-validation-rules">可用的验证规则</a></li>
<li><a href="#conditionally-adding-rules">按条件添加验证规则</a></li>
<li><a href="#validating-arrays">验证数组</a>
<ul>
<li><a href="#validating-nested-array-input">验证多维数组</a></li>
<li><a href="#error-message-indexes-and-positions">错误消息的索引和定位</a></li>
</ul>
</li>
<li><a href="#validating-files">验证文件</a></li>
<li><a href="#validating-passwords">验证密码</a></li>
<li><a href="#custom-validation-rules">自定义验证规则</a>
<ul>
<li><a href="#using-rule-objects">使用 Rule 对象</a></li>
<li><a href="#using-closures">使用闭包函数</a></li>
<li><a href="#implicit-rules">隐式规则</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 提供了几种不同的方法来验证传入应用程序的数据。最常见的做法是在所有传入的 HTTP 请求中使用 <code v-pre>validate</code> 方法。同时，我们还将讨论其他验证方法。</p>
<p>Laravel 包含了各种方便的验证规则，你可以将它们应用于数据，甚至可以验证给定数据库表中的值是否唯一。我们将详细介绍每个验证规则，以便你熟悉 Laravel 的所有验证功能。</p>
<h2 id="快速开始" tabindex="-1"><a class="header-anchor" href="#快速开始"><span>快速开始</span></a></h2>
<p>为了了解 Laravel 强大的验证功能，我们来看一个表单验证并将错误消息展示给用户的完整示例。通过阅读概述，这将会对你如何使用 Laravel 验证传入的请求数据有一个很好的理解：</p>
<h3 id="定义路由" tabindex="-1"><a class="header-anchor" href="#定义路由"><span>定义路由</span></a></h3>
<p>首先，假设我们在 <code v-pre>routes/web.php</code> 路由文件中定义了下面这些路由：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>PostController</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/post/create'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token class-name static-context">PostController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'create'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/post'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token class-name static-context">PostController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'store'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>GET</code> 路由会显示一个供用户创建新博客文章的表单，而 <code v-pre>POST</code> 路由会将新的博客文章存储到数据库中。</p>
<h3 id="创建控制器" tabindex="-1"><a class="header-anchor" href="#创建控制器"><span>创建控制器</span></a></h3>
<p>接下来，让我们一起来看看处理这些路由的简单控制器。我们暂时留空了 <code v-pre>store</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>RedirectResponse</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>View<span class="token punctuation">\</span>View</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">PostController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 博客的表单视图</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">create</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">View</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post.create'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 存储博客的 Action</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RedirectResponse</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// 验证并且执行存储逻辑</span></span>
<span class="line"></span>
<span class="line">        <span class="token variable">$post</span> <span class="token operator">=</span> <span class="token doc-comment comment">/** ... */</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">to_route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post.show'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'post'</span> <span class="token operator">=></span> <span class="token variable">$post</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="编写验证逻辑" tabindex="-1"><a class="header-anchor" href="#编写验证逻辑"><span>编写验证逻辑</span></a></h3>
<p>现在我们开始在 <code v-pre>store</code> 方法中编写用来验证新的博客文章的逻辑代码。为此，我们将使用 <code v-pre>Illuminate\Http\Request</code> 类提供的 <code v-pre>validate</code> 方法。如果验证通过，你的代码会继续正常运行。如果验证失败，则会抛出 <code v-pre>Illuminate\Validation\ValidationException</code> 异常，并自动将对应的错误响应返回给用户。</p>
<p>如果在传统的HTTP请求中验证失败，将会生成到前一个URL的重定向响应。如果传入的请求是XHR请求，将返回一个 <a href="#validation-error-response-format">包含验证错误消息的JSON响应</a> 。</p>
<p>为了更好地理解 <code v-pre>validate</code> 方法，让我们跳回到 <code v-pre>store</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 存储一篇新的博客文章。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RedirectResponse</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 博客文章有效...</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/posts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如你所见，验证规则被传入到<code v-pre>validate</code>方法中。不用担心 - 所有可用的验证规则都已经在<a href="#available-validation-rules">文档</a>中。再次强调，如果验证失败，将自动生成适当的响应。如果验证通过，我们的控制器将正常执行。</p>
<p>另外，验证规则也可以以规则数组的形式指定，而不是单个<code v-pre>|</code> 分隔的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validatedData</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'unique:posts'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'max:255'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，你可以使用 <code v-pre>validateWithBag</code> 方法来验证请求，并将任何错误消息存储在一个<a href="#named-error-bags">命名错误包中</a>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validatedData</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validateWithBag</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'unique:posts'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'max:255'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="在首次验证失败时停止运行" tabindex="-1"><a class="header-anchor" href="#在首次验证失败时停止运行"><span>在首次验证失败时停止运行</span></a></h4>
<p>有时，您可能希望在第一次验证失败后停止对属性运行验证规则。为此，将 <code v-pre>bail</code> 规则分配给该属性:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bail|required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在这个例子中，如果 <code v-pre>title</code> 属性上的 <code v-pre>unique</code> 规则失败，<code v-pre>max</code> 规则将不会被检查。规则将按照分配的顺序进行验证。</p>
<h4 id="嵌套字段的说明" tabindex="-1"><a class="header-anchor" href="#嵌套字段的说明"><span>嵌套字段的说明</span></a></h4>
<p>如果传入的 HTTP 请求包含「嵌套」参数，你可以在验证规则中使用<code v-pre>.</code>语法来指定这些参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'author.name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'author.description'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>另外，如果你的字段名称包含点，则可以通过使用反斜杠将点转义，以防止将其解释为<code v-pre>.</code>语法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'v1\.0'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="显示验证错误信息" tabindex="-1"><a class="header-anchor" href="#显示验证错误信息"><span>显示验证错误信息</span></a></h3>
<p>那么，如果传入的请求字段没有通过验证规则呢？如前所述，Laravel 会自动将用户重定向到之前的位置。此外，所有的验证错误和 <a href="https://learnku.com/docs/laravel/11.x/requestsmd/16661#input" target="_blank" rel="noopener noreferrer">请求输入</a> 都会自动存入到 <a href="https://learnku.com/docs/laravel/11.x/sessionmd/16667#flash-data" target="_blank" rel="noopener noreferrer">闪存 session</a> 中。</p>
<p><code v-pre>$errors</code> 变量由<code v-pre>Illuminate\View\Middleware\ShareErrorsFromSession</code> 中间件与应用程序的所有视图共享，该变量由 <code v-pre>web</code> 中间件组提供。当应用该中间件时，<code v-pre>$errors</code> 变量始终在视图中可用，这使您可以方便地假设 <code v-pre>$errors</code> 变量始终是定义的并且可以安全地使用。<code v-pre>$errors</code> 变量是 <code v-pre>Illuminate\Support\MessageBag</code> 的实例. 更多有关使用该对象的信息，请 <a href="#working-with-error-messages">查看文档</a>.</p>
<p>因此，在我们的示例中，当验证失败时，用户将重定向到控制器 <code v-pre>create</code> 方法，从而在视图中显示错误消息：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;!-- /resources/views/post/create.blade.php --&gt;</span>
<span class="line"></span>
<span class="line">&lt;h1&gt;Create Post&lt;/h1&gt;</span>
<span class="line"></span>
<span class="line">@if ($errors-&gt;any())</span>
<span class="line">    &lt;div class=&quot;alert alert-danger&quot;&gt;</span>
<span class="line">        &lt;ul&gt;</span>
<span class="line">            @foreach ($errors-&gt;all() as $error)</span>
<span class="line">                &lt;li&gt;{{ $error }}&lt;/li&gt;</span>
<span class="line">            @endforeach</span>
<span class="line">        &lt;/ul&gt;</span>
<span class="line">    &lt;/div&gt;</span>
<span class="line">@endif</span>
<span class="line"></span>
<span class="line">&lt;!-- 创建博客表单 --&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义错误消息" tabindex="-1"><a class="header-anchor" href="#自定义错误消息"><span>自定义错误消息</span></a></h4>
<p>Laravel 内置的每个验证规则都有一个错误消息，位于你的应用程序的 <code v-pre>lang/en/validation.php</code> 文件中。如果你的应用程序没有 <code v-pre>lang</code> 目录，你可以指示 Laravel 使用 <code v-pre>lang:publish</code> Artisan命令创建它。</p>
<p>在 <code v-pre>lang/en/validation.php</code> 文件中，你会找到每个验证规则的翻译条目。根据你的应用程序的需要，你可以自由更改或修改这些消息。</p>
<p>此外，你可以复制这个文件到另一个语言目录来翻译你的应用程序的语言消息。要了解更多关于 Laravel 本地化的信息，请查看完整的 <a href="https://learnku.com/docs/laravel/11.x/localizationmd/16681" target="_blank" rel="noopener noreferrer">本地化文档</a>。</p>
<blockquote>
<p><strong>注意</strong><br>
默认情况下，Laravel 应用程序框架不包括 <code v-pre>lang</code> 目录。如果你想自定义 Laravel 的语言文件，你可以通过 <code v-pre>lang:publish</code> Artisan 命令发布它们。</p>
</blockquote>
<h4 id="xhr-请求和验证" tabindex="-1"><a class="header-anchor" href="#xhr-请求和验证"><span>XHR 请求和验证</span></a></h4>
<p>在这个示例中，我们使用传统表单向应用发送数据。然而，许多应用程序从由 JavaScript 驱动的前端接收 XHR 请求。在 XHR 请求中使用 <code v-pre>validate</code> 方法时，Laravel 不会生成重定向响应。相反，Laravel 会生成一个包含所有验证错误的 <a href="#validation-error-response-format">JSON 响应</a>。这个 JSON 响应会以 422 HTTP 状态码发送。</p>
<h4 id="error-指令" tabindex="-1"><a class="header-anchor" href="#error-指令"><span><code v-pre>@error</code> 指令</span></a></h4>
<p>你可以使用 <code v-pre>@error</code> <a href="https://learnku.com/docs/laravel/11.x/blademd/16664" target="_blank" rel="noopener noreferrer">Blade</a> 指令快速确定某个属性是否存在验证错误消息。 在 <code v-pre>@error</code> 指令内部，你可以回显 <code v-pre>$message</code> 变量来显示错误消息</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;!-- /resources/views/post/create.blade.php --&gt;</span>
<span class="line"></span>
<span class="line">&lt;label for=&quot;title&quot;&gt;文章标题&lt;/label&gt;</span>
<span class="line"></span>
<span class="line">&lt;input id=&quot;title&quot;</span>
<span class="line">    type=&quot;text&quot;</span>
<span class="line">    name=&quot;title&quot;</span>
<span class="line">    class=&quot;@error('title') is-invalid @enderror&quot;&gt;</span>
<span class="line"></span>
<span class="line">@error('title')</span>
<span class="line">    &lt;div class=&quot;alert alert-danger&quot;&gt;{{ $message }}&lt;/div&gt;</span>
<span class="line">@enderror</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您使用 <a href="#named-error-bags">命名错误包</a>，您可以将错误包的名称作为第二个参数传递给 <code v-pre>@error</code> 指令:</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;input ... class=&quot;@error('title', 'post') is-invalid @enderror&quot;&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="回填表单" tabindex="-1"><a class="header-anchor" href="#回填表单"><span>回填表单</span></a></h3>
<p>当 Laravel 由于验证错误而生成重定向响应时，框架将自动 <a href="https://learnku.com/docs/laravel/11.x/sessionmd/16667#flash-data" target="_blank" rel="noopener noreferrer">闪存所有请求的输入到 session 中</a>。这样做是为了在下一个请求期间方便地访问输入，并重新填充用户尝试提交的表单。</p>
<p>若要从上一个请求中检索闪存的输入， 请在 <code v-pre>Illuminate\Http\Request</code> 类的实例上调用 <code v-pre>old</code> 方法。 <code v-pre>old</code> 方法将从 <a href="https://learnku.com/docs/laravel/11.x/sessionmd/16667" target="_blank" rel="noopener noreferrer">session</a> 中提取之前闪存的输入数据：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$title</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'title'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>Laravel 还提供了一个全局性的 <code v-pre>old</code> 助手函数. 如果您要在 <a href="https://learnku.com/docs/laravel/11.x/blademd/16664" target="_blank" rel="noopener noreferrer">Blade 模板</a> 中显示旧输入数据，使用 <code v-pre>old</code> 助手函数来重新填充表单会更加方便。如果给定字段不存在旧的输入数据，则将返回 <code v-pre>null</code> ：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">&lt;input type=&quot;text&quot; name=&quot;title&quot; value=&quot;{{ old('title') }}&quot;&gt;</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="关于可选字段的注意事项" tabindex="-1"><a class="header-anchor" href="#关于可选字段的注意事项"><span>关于可选字段的注意事项</span></a></h3>
<p>默认情况下，Laravel 在应用程序的全局中间件堆栈中包含 <code v-pre>TrimStrings</code> 和 <code v-pre>ConvertEmptyStringsToNull</code> 中间件。因此，如果您不希望 <code v-pre>null</code> 值被验证器标识为非法的话，您需要将「可选」字段标记为 <code v-pre>nullable</code>。例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'publish_at'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'nullable|date'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在此示例中，我们指定 <code v-pre>publish_at</code> 字段可以为 <code v-pre>null</code> 或有效的日期表示。如果没有将 <code v-pre>nullable</code> 修饰符添加到规则定义中，则验证器会将 <code v-pre>null</code> 视为无效日期。</p>
<h3 id="验证错误响应格式" tabindex="-1"><a class="header-anchor" href="#验证错误响应格式"><span>验证错误响应格式</span></a></h3>
<p>当您的应用程序抛出 <code v-pre>Illuminate\Validation\ValidationException</code> 异常，并且传入的 HTTP 请求希望返回 JSON 响应时，Laravel 将自动为您格式化错误消息，并返回 <code v-pre>422 Unprocessable Entity HTTP</code> 响应。</p>
<p>下面是验证错误的 JSON 响应格式示例。请注意，嵌套的错误键会被转换为 “点” 符号格式：</p>
<div class="language-json line-numbers-mode" data-highlighter="prismjs" data-ext="json" data-title="json"><pre v-pre class="language-json"><code><span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token property">"message"</span><span class="token operator">:</span> <span class="token string">"The team name must be a string. (and 4 more errors)"</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token property">"errors"</span><span class="token operator">:</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token property">"team_name"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string">"The team name must be a string."</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string">"The team name must be at least 1 characters."</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"authorization.role"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string">"The selected authorization.role is invalid."</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"users.0.email"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string">"The users.0.email field is required."</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token property">"users.2.email"</span><span class="token operator">:</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string">"The users.2.email must be a valid email address."</span></span>
<span class="line">        <span class="token punctuation">]</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="表单请求验证" tabindex="-1"><a class="header-anchor" href="#表单请求验证"><span>表单请求验证</span></a></h2>
<h3 id="创建表单请求" tabindex="-1"><a class="header-anchor" href="#创建表单请求"><span>创建表单请求</span></a></h3>
<p>对于更复杂的验证场景，您可能希望创建一个 “表单请求”。表单请求是自定义请求类，封装了自己的验证和授权逻辑。要创建一个表单请求类，您可以使用 <code v-pre>make:request</code> Artisan CLI 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:request StorePostRequest</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>生成的表单请求类将被放置在 <code v-pre>app/Http/Requests</code> 目录中。如果此目录不存在，则在运行 <code v-pre>make:request</code> 命令时将创建该目录。Laravel 生成的每个表单请求都有两个方法：<code v-pre>authorize</code> 和 <code v-pre>rules</code>。</p>
<p>正如您可能猜到的，<code v-pre>authorize</code> 方法负责确定当前已认证的用户是否可以执行请求所代表的操作，而 <code v-pre>rules</code> 方法则返回应该应用于请求数据的验证规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取应用于请求的验证规则。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, \Illuminate\Contracts\Validation\Rule|array|string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">rules</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>技巧</strong><br>
您可以在 <code v-pre>rules</code> 方法的签名中类型提示任何您需要的依赖项。它们将通过 Laravel <a href="https://learnku.com/docs/laravel/11.x/containermd/16654" target="_blank" rel="noopener noreferrer">服务容器</a> 自动解析。</p>
</blockquote>
<p>那么，验证规则是如何被评估的呢？您只需要在控制器方法中对请求进行类型提示。传入的表单请求会在控制器方法被调用之前进行验证，这意味着您不需要在控制器中添加任何验证逻辑：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 存储新的博客文章。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">StorePostRequest</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RedirectResponse</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 传入的请求是有效的...</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 检索验证过的输入数据...</span></span>
<span class="line">    <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validated</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 检索部分验证过的输入数据...</span></span>
<span class="line">    <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// 存储博客文章...</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/posts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果验证失败，将生成一个重定向响应，将用户送回他们之前的位置。错误信息也会被闪存到 session 中，以便显示。如果请求是 XHR 请求，将向用户返回一个 HTTP 422 状态码的响应，其中包含 <a href="#validation-error-response-format">验证错误的 JSON 表示</a>。</p>
<blockquote>
<p><strong>技巧</strong><br>
需要为您的 Inertia 驱动的 Laravel 前端添加实时表单请求验证吗？ 请查看 <a href="https://learnku.com/docs/laravel/11.x/precognitionmd/16727" target="_blank" rel="noopener noreferrer">Laravel实时服务器端表单验证扩展</a>.</p>
</blockquote>
<h4 id="执行附加验证" tabindex="-1"><a class="header-anchor" href="#执行附加验证"><span>执行附加验证</span></a></h4>
<p>有时，在完成初始验证后，你需要执行额外的验证。您可以使用表单请求的<code v-pre>after</code>方法来完成此操作。</p>
<p><code v-pre>after</code> 方法应返回一个可调用或闭包数组，这些数组将在验证完成后调用。给定的可调用程序将接收一个<code v-pre>Illuminate\Validation\Validator</code>实例，允许你在必要时引发其他错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取请求的 after 验证可调用项。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">after</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Validator</span> <span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">somethingElseIsInvalid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">errors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span></span>
<span class="line">                    <span class="token string single-quoted-string">'field'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'Something is wrong with this field!'</span></span>
<span class="line">                <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如前所述，<code v-pre>after</code> 方法返回的数组也可能包含可调用的类。这些类的 <code v-pre>__invoke</code> 方法将接收一个<code v-pre>Illuminate\Validation\Validator</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidateShippingTime</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidateUserStatus</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取请求的“after”验证可调用项。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">after</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token keyword">new</span> <span class="token class-name">ValidateUserStatus</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">new</span> <span class="token class-name">ValidateShippingTime</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Validator</span> <span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">//</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="首次验证失败时停止" tabindex="-1"><a class="header-anchor" href="#首次验证失败时停止"><span>首次验证失败时停止</span></a></h4>
<p>通过将 <code v-pre>stopOnFirstFailure</code> 属性添加到请求类，你可以通知验证器，一旦发生单个验证失败，它就应该停止验证所有属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 指示验证程序是否应在第一个规则失败时停止。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">bool</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$stopOnFirstFailure</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义重定向位置" tabindex="-1"><a class="header-anchor" href="#自定义重定向位置"><span>自定义重定向位置</span></a></h4>
<p>如前所述，当表单请求验证失败时，将生成重定向响应，将用户发送回其以前的位置。但是，你可以自由地自定义此行为。因此，请在表单请求中定义 <code v-pre>$redirect</code> 属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 如果验证失败，则用户应重定向到的URI。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$redirect</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="执行额外的验证" tabindex="-1"><a class="header-anchor" href="#执行额外的验证"><span>执行额外的验证</span></a></h4>
<p>有时，在初始验证完成后，您可能需要执行额外的验证。您可以使用表单请求的 <code v-pre>after</code> 方法来实现这一点。</p>
<p><code v-pre>after</code> 方法应返回一个可调用对象或闭包的数组，这些对象或闭包将在验证完成后被调用。给定的可调用对象将接收一个 <code v-pre>Illuminate\Validation\Validator</code> 实例，允许您在必要时添加额外的错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取请求的「after」验证可调用对象。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">after</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Validator</span> <span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">somethingElseIsInvalid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">errors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span></span>
<span class="line">                    <span class="token string single-quoted-string">'field'</span><span class="token punctuation">,</span></span>
<span class="line">                    <span class="token string single-quoted-string">'Something is wrong with this field!'</span></span>
<span class="line">                <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如上所述， <code v-pre>after</code> 方法返回的数组也可以包含可调用类。这些类的 <code v-pre>__invoke</code> 方法将接收一个 <code v-pre>Illuminate\Validation\Validator</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidateShippingTime</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidateUserStatus</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取请求的「after」验证回调函数。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">after</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token keyword">new</span> <span class="token class-name">ValidateUserStatus</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">new</span> <span class="token class-name">ValidateShippingTime</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Validator</span> <span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token comment">//</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="在首次验证失败时停止" tabindex="-1"><a class="header-anchor" href="#在首次验证失败时停止"><span>在首次验证失败时停止</span></a></h4>
<p>通过在请求类中添加 <code v-pre>stopOnFirstFailure</code> 属性，您可以告知验证器在出现单个验证失败后应停止验证所有属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 指示验证器是否应该在第一个规则失败时停止。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">bool</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$stopOnFirstFailure</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义重定向位置-1" tabindex="-1"><a class="header-anchor" href="#自定义重定向位置-1"><span>自定义重定向位置</span></a></h4>
<p>如前所述，当表单请求验证失败时，将生成一个重定向响应，将用户送回他们之前的位置。然而，您可以自由地自定义这个行为。要做到这一点，请在您的表单请求中定义一个 <code v-pre>$redirect</code> 属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 如果验证失败，用户应该被重定向到的 URI。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$redirect</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/dashboard'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，如果您想将用户重定向到命名路由，您可以定义一个 <code v-pre>$redirectRoute</code> 属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 如果验证失败，用户应该被重定向到的路由。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">string</span></span></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token variable">$redirectRoute</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'dashboard'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="授权表单请求" tabindex="-1"><a class="header-anchor" href="#授权表单请求"><span>授权表单请求</span></a></h3>
<p>表单请求类还包含一个 <code v-pre>authorize</code> 方法。在这个方法中，您可以确定已认证的用户是否实际有权更新给定的资源。例如，您可以确定用户是否真的拥有他们试图更新的博客评论。通常，您会在这个方法中与您的 <a href="https://learnku.com/docs/laravel/11.x/authorization" target="_blank" rel="noopener noreferrer">授权门和策略</a> 进行交互：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>Comment</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 确定用户是否有权进行此请求。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">authorize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">bool</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$comment</span> <span class="token operator">=</span> <span class="token class-name static-context">Comment</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comment'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$comment</span> <span class="token operator">&amp;&amp;</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update'</span><span class="token punctuation">,</span> <span class="token variable">$comment</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于所有表单请求都继承自 Laravel 的基础请求类，我们可以使用 <code v-pre>user</code> 方法来访问当前已认证的用户。另外，注意上面示例中对 <code v-pre>route</code> 方法的调用。这个方法允许您访问在被调用路由上定义的 URI 参数，比如下面示例中的 <code v-pre>{comment}</code> 参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Route</span><span class="token operator">::</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/comment/{comment}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>因此，如果您的应用程序利用了 <a href="https://learnku.com/docs/laravel/11.x/routing#route-model-binding" target="_blank" rel="noopener noreferrer">路由模型绑定</a>，您的代码可以通过将解析后的模型作为请求的属性来访问，从而变得更加简洁：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">can</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'update'</span><span class="token punctuation">,</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">comment</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果 <code v-pre>authorize</code> 方法返回 <code v-pre>false</code>，将自动返回一个 HTTP 403 状态码的响应，并且您的控制器方法不会被执行。</p>
<p>如果您计划在应用程序的其他部分处理请求的授权逻辑，您可以完全移除 <code v-pre>authorize</code> 方法，或者简单地返回 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 确定用户是否有权进行此请求。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">authorize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">bool</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>技巧</strong><br>
您可以在 <code v-pre>authorize</code> 方法的签名中类型提示任何需要的依赖项。它们将通过 Laravel <a href="https://learnku.com/docs/laravel/11.x/container" target="_blank" rel="noopener noreferrer">服务容器</a>自动解析。</p>
</blockquote>
<h3 id="自定义错误消息-1" tabindex="-1"><a class="header-anchor" href="#自定义错误消息-1"><span>自定义错误消息</span></a></h3>
<p>您可以通过重写 <code v-pre>messages</code> 方法来自定义表单请求使用的错误消息。此方法应返回一个属性/规则对及其对应错误消息的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取定义的验证规则的错误消息。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">messages</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'title.required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'A title is required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'body.required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'A message is required'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="自定义验证属性" tabindex="-1"><a class="header-anchor" href="#自定义验证属性"><span>自定义验证属性</span></a></h4>
<p>Laravel 的许多内置验证规则错误消息包含 <code v-pre>:attribute</code> 占位符。如果您希望将验证消息中的 <code v-pre>:attribute</code> 占位符替换为自定义属性名，可以通过重写 <code v-pre>attributes</code> 方法来指定自定义名称。此方法应返回一个属性/名称对的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 获取验证器错误的自定义属性。</span>
<span class="line"> *</span>
<span class="line"> * <span class="token keyword">@return</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, string></span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">attributes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">array</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email address'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="准备验证输入" tabindex="-1"><a class="header-anchor" href="#准备验证输入"><span>准备验证输入</span></a></h3>
<p>如果您需要在应用验证规则之前准备或清理请求中的任何数据，可以使用 <code v-pre>prepareForValidation</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 准备验证数据。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">prepareForValidation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">merge</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'slug'</span> <span class="token operator">=></span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">slug</span><span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token property">slug</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>同样，如果您需要在验证完成后规范化任何请求数据，可以使用 <code v-pre>passedValidation</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 处理通过的验证尝试。</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">protected</span> <span class="token keyword">function</span> <span class="token function-definition function">passedValidation</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="手动创建验证器" tabindex="-1"><a class="header-anchor" href="#手动创建验证器"><span>手动创建验证器</span></a></h2>
<p>如果您不想在请求上使用 <code v-pre>validate</code> 方法，可以使用 <code v-pre>Validator</code> <a href="https://learnku.com/docs/laravel/11.x/facades" target="_blank" rel="noopener noreferrer">门面</a>手动创建一个验证器实例。门面上的 <code v-pre>make</code> 方法生成一个新的验证器实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>RedirectResponse</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Request</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">PostController</span> <span class="token keyword">extends</span> <span class="token class-name">Controller</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 存储新的博客文章。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">store</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Request</span> <span class="token variable">$request</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token class-name return-type">RedirectResponse</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">fails</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post/create'</span><span class="token punctuation">)</span></span>
<span class="line">                        <span class="token operator">-></span><span class="token function">withErrors</span><span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">)</span></span>
<span class="line">                        <span class="token operator">-></span><span class="token function">withInput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 获取验证后的输入...</span></span>
<span class="line">        <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">validated</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 获取验证后输入的一部分...</span></span>
<span class="line">        <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token comment">// 存储博客文章...</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/posts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>传递给 <code v-pre>make</code> 方法的第一个参数是要验证的数据。第二个参数是应用于数据的验证规则数组。</p>
<p>在确定请求验证是否失败后，您可以使用 <code v-pre>withErrors</code> 方法将错误消息闪存到会话中。使用此方法时， <code v-pre>$errors</code>变量将在重定向后自动与您的视图共享，允许您轻松地将它们显示给用户。 <code v-pre>withErrors</code> 方法接受一个验证器、一个 <code v-pre>MessageBag</code> 或一个 PHP <code v-pre>array</code>。</p>
<h4 id="首次验证失败时停止-1" tabindex="-1"><a class="header-anchor" href="#首次验证失败时停止-1"><span>首次验证失败时停止</span></a></h4>
<p><code v-pre>stopOnFirstFailure</code> 方法将通知验证器，一旦发生单个验证失败，它就会停止验证所有属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">stopOnFirstFailure</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">fails</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="自动重定向" tabindex="-1"><a class="header-anchor" href="#自动重定向"><span>自动重定向</span></a></h3>
<p>如果你想手动创建一个验证程序实例，但仍然利用HTTP请求的 <code v-pre>validate</code> 方法提供的自动重定向，则可以在现有的验证程序实例上调用 <code v-pre>validate</code> 方法。如果验证失败，用户将自动重定向，或者在XHR请求的情况下，将<a href="#%E9%AA%8C%E8%AF%81%E9%94%99%E8%AF%AF%E5%93%8D%E5%BA%94%E6%A0%BC%E5%BC%8F">返回JSON响应</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果验证失败，你可以使用 <code v-pre>validateWithBag</code> 方法将错误消息存储在<a href="#%E5%91%BD%E5%90%8D%E9%94%99%E8%AF%AF%E5%8C%85">命名错误包</a>中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|unique:posts|max:255'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'body'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">validateWithBag</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'post'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="命名错误包" tabindex="-1"><a class="header-anchor" href="#命名错误包"><span>命名错误包</span></a></h3>
<p>如果你在一个页面上有多个表单，可能希望命名包含验证错误的 <code v-pre>MessageBag</code> ，以便检索特定表单的错误消息。要实现这一点，请将名称作为第二个参数传递给 <code v-pre>withErrors</code> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'register'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">withErrors</span><span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>然后，你可以从 <code v-pre>$errors</code> 变量访问命名的 <code v-pre>MessageBag</code> 实例：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">{{ $errors-&gt;login-&gt;first('email') }}</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="自定义错误消息-2" tabindex="-1"><a class="header-anchor" href="#自定义错误消息-2"><span>自定义错误消息</span></a></h3>
<p>如果需要，你可以提供验证器实例应该使用的自定义错误消息，而不是Laravel提供的默认错误消息。有几种方法可以指定自定义消息。首先，你可以将自定义消息作为第三个参数传递给<code v-pre>Validator::make</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">,</span> <span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute field is required.'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在本例中，<code v-pre>:attribute</code> 占位符将替换为正在验证的字段的实际名称。你也可以在验证消息中使用其他占位符。例如</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'same'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute and :other must match.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'size'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute must be exactly :size.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'between'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute value :input is not between :min - :max.'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'in'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'The :attribute must be one of the following types: :values'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="给自定义消息指定属性" tabindex="-1"><a class="header-anchor" href="#给自定义消息指定属性"><span>给自定义消息指定属性</span></a></h4>
<p>有时，你可能希望仅为特定属性指定自定义错误消息。您可以使用点 <code v-pre>.</code> 表示法。首先指定属性的名称，然后指定规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$messages</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email.required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'We need to know your email address!'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="指定自定义属性值" tabindex="-1"><a class="header-anchor" href="#指定自定义属性值"><span>指定自定义属性值</span></a></h4>
<p>Laravel的许多内置错误消息都包括一个 <code v-pre>:attribute</code> 占位符，该占位符被替换为正在验证的字段或属性的名称。要自定义用于替换特定字段的占位符的值，可以将自定义属性数组作为第四个参数传递给 <code v-pre>Validator:：make</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">,</span> <span class="token variable">$messages</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email address'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="执行附加验证-1" tabindex="-1"><a class="header-anchor" href="#执行附加验证-1"><span>执行附加验证</span></a></h3>
<p>有时，在完成初始验证后，你需要执行额外的验证。你可以使用验证器的 <code v-pre>after</code> 方法来实现这一点。 <code v-pre>after</code> 方法接受一个闭包或一个可调用数组，这些闭包或数组将在验证完成后调用。给定的可调用程序将接收一个 <code v-pre>Illuminate\Validation\Validator</code> 实例，允许你在必要时引发其他错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token comment">/* ... */</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$this</span><span class="token operator">-></span><span class="token function">somethingElseIsInvalid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">errors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">add</span><span class="token punctuation">(</span></span>
<span class="line">            <span class="token string single-quoted-string">'field'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Something is wrong with this field!'</span></span>
<span class="line">        <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">fails</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如所述， <code v-pre>after</code> 方法也接受一个回调函数数组，这在您的「后置验证」逻辑封装在可调用类中时特别方便，这些类将通过其 <code v-pre>__invoke</code> 方法接收一个 <code v-pre>Illuminate\Validation\Validator</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidateShippingTime</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidateUserStatus</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token keyword">new</span> <span class="token class-name">ValidateUserStatus</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">new</span> <span class="token class-name">ValidateShippingTime</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token comment">// ...</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="处理已验证的输入" tabindex="-1"><a class="header-anchor" href="#处理已验证的输入"><span>处理已验证的输入</span></a></h2>
<p>使用表单请求或手动创建的验证器实例验证传入的请求数据后，您可能希望检索实际经过验证的传入请求数据。这可以通过几种方式完成。首先，您可以在表单请求或验证器实例上调用 <code v-pre>validated</code> 方法。此方法返回已验证数据的数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validated</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">validated</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，您可以在表单请求或验证器实例上调用 <code v-pre>safe</code> 方法。此方法返回 <code v-pre>Illuminate\Support\ValidatedInput</code>的实例。此对象仅公开 <code v-pre>only</code>, <code v-pre>except</code> 和 <code v-pre>all</code> 方法，用于检索已验证数据的子集或全部已验证数据数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外， <code v-pre>Illuminate\Support\ValidatedInput</code> 实例可以像数组一样被迭代和访问：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 已验证的数据可以被迭代...</span></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$key</span> <span class="token operator">=></span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 已验证的数据可以作为数组访问...</span></span>
<span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$email</span> <span class="token operator">=</span> <span class="token variable">$validated</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您想向已验证的数据添加额外的字段，可以调用 <code v-pre>merge</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validated</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">merge</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果您想将已验证的数据作为 <a href="https://learnku.com/docs/laravel/11.x/collectionsmd/16674" target="_blank" rel="noopener noreferrer">集合</a> 实例检索，可以调用 <code v-pre>collect</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">safe</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="处理错误消息" tabindex="-1"><a class="header-anchor" href="#处理错误消息"><span>处理错误消息</span></a></h2>
<p>在 <code v-pre>Validator</code> 实例上调用 <code v-pre>errors</code> 方法后，您将收到一个 <code v-pre>Illuminate\Support\MessageBag</code> 实例，它有多种方便的方法来处理错误消息。自动提供给所有视图的 <code v-pre>$errors</code>变量也是 <code v-pre>MessageBag</code> 类的实例。</p>
<h4 id="检索字段的第一个错误消息" tabindex="-1"><a class="header-anchor" href="#检索字段的第一个错误消息"><span>检索字段的第一个错误消息</span></a></h4>
<p>要检索给定字段的第一个错误消息，请使用 <code v-pre>first</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$errors</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">errors</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">first</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="检索字段的所有错误消息" tabindex="-1"><a class="header-anchor" href="#检索字段的所有错误消息"><span>检索字段的所有错误消息</span></a></h4>
<p>如果您需要检索给定字段的所有消息数组，请使用 <code v-pre>get</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果您正在验证数组表单字段，可以使用 <code v-pre>*</code> 字符检索数组中每个元素的所有消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'attachments.*'</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="检索所有字段的所有错误消息" tabindex="-1"><a class="header-anchor" href="#检索所有字段的所有错误消息"><span>检索所有字段的所有错误消息</span></a></h4>
<p>要检索所有字段的所有消息数组，请使用 <code v-pre>all</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token variable">$message</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="确定字段是否存在消息" tabindex="-1"><a class="header-anchor" href="#确定字段是否存在消息"><span>确定字段是否存在消息</span></a></h4>
<p><code v-pre>has</code> 方法可用于确定给定字段是否存在任何错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$errors</span><span class="token operator">-></span><span class="token function">has</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'email'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="在语言文件中指定自定义消息" tabindex="-1"><a class="header-anchor" href="#在语言文件中指定自定义消息"><span>在语言文件中指定自定义消息</span></a></h3>
<p>Laravel 的内置验证规则都有一个错误消息，位于应用程序的 <code v-pre>lang/en/validation.php</code>文件中。如果您的应用程序没有 <code v-pre>lang</code> 目录，可以使用 <code v-pre>lang:publish</code> Artisan 命令指示 Laravel 创建它。</p>
<p>在 <code v-pre>lang/en/validation.php</code> 文件中，您会找到每个验证规则的翻译条目。您可以根据应用程序的需求自由更改或修改这些消息。</p>
<p>此外，您可以将此文件复制到另一个语言目录，以翻译应用程序语言的消息。要了解有关 Laravel 本地化的更多信息，请查看完整的 <a href="https://learnku.com/docs/laravel/11.x/localizationmd/16681" target="_blank" rel="noopener noreferrer">本地化文档</a>。</p>
<blockquote>
<p><strong>警告</strong><br>
默认情况下，Laravel 应用程序骨架不包含 <code v-pre>lang</code> 目录。如果您想自定义 Laravel 的语言文件，可以通过 <code v-pre>lang:publish</code> Artisan 命令发布它们。</p>
</blockquote>
<h4 id="特定属性的自定义消息" tabindex="-1"><a class="header-anchor" href="#特定属性的自定义消息"><span>特定属性的自定义消息</span></a></h4>
<p>您可以在应用程序的验证语言文件中自定义用于指定属性和规则组合的错误消息。为此，请将您的消息自定义添加到应用程序的 <code v-pre>lang/xx/validation.php</code> 语言文件的 <code v-pre>custom</code> 数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'custom'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'We need to know your email address!'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'max'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Your email address is too long!'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="在语言文件中指定属性" tabindex="-1"><a class="header-anchor" href="#在语言文件中指定属性"><span>在语言文件中指定属性</span></a></h3>
<p>Laravel 的许多内置错误消息包含一个 <code v-pre>:attribute</code> 占位符，它会被替换为正在验证的字段或属性的名称。如果您希望验证消息的 <code v-pre>:attribute</code>部分被替换为自定义值，可以在 <code v-pre>lang/xx/validation.php</code> 语言文件的 <code v-pre>attributes</code> 数组中指定自定义属性名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'attributes'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email address'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>警告</strong><br>
默认情况下，Laravel 应用程序骨架不包含 <code v-pre>lang</code> 目录。如果您想自定义 Laravel 的语言文件，可以通过 <code v-pre>lang:publish</code> Artisan 命令发布它们。</p>
</blockquote>
<h3 id="在语言文件中指定值" tabindex="-1"><a class="header-anchor" href="#在语言文件中指定值"><span>在语言文件中指定值</span></a></h3>
<p>Laravel 的一些内置验证规则错误消息包含一个 <code v-pre>:value</code> 占位符，它会被替换为请求属性的当前值。但是，有时您可能需要将验证消息的 <code v-pre>:value</code> 部分替换为值的自定义表示。例如，考虑以下规则，该规则指定当 <code v-pre>payment_type</code> 的值为 <code v-pre>cc</code> 时，需要提供信用卡号码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'credit_card_number'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required_if:payment_type,cc'</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果此验证规则失败，它将产生以下错误消息：</p>
<div class="language-none line-numbers-mode" data-highlighter="prismjs" data-ext="none" data-title="none"><pre v-pre class="language-none"><code><span class="line">当支付类型为 cc 时，信用卡号码字段是必填的。</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>您可以在 <code v-pre>lang/xx/validation.php</code> 语言文件中定义一个 <code v-pre>values</code> 数组，以指定一个更加用户友好的值表示，而不是显示 <code v-pre>cc</code> 作为支付类型值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'values'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'payment_type'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'cc'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'credit card'</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>警告</strong><br>
默认情况下，Laravel 应用程序骨架不包含 <code v-pre>lang</code> 目录。如果您想自定义 Laravel 的语言文件，可以通过 <code v-pre>lang:publish</code> Artisan 命令发布它们。</p>
</blockquote>
<p>定义此值后，验证规则将产生以下错误消息：</p>
<div class="language-none line-numbers-mode" data-highlighter="prismjs" data-ext="none" data-title="none"><pre v-pre class="language-none"><code><span class="line">当支付类型为信用卡时，信用卡号码字段是必填的。</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="可用的验证规则" tabindex="-1"><a class="header-anchor" href="#可用的验证规则"><span>可用的验证规则</span></a></h2>
<p>以下是所有可用验证规则及其功能的列表：</p>
<h4 id="accepted" tabindex="-1"><a class="header-anchor" href="#accepted"><span>accepted</span></a></h4>
<p>待验证的字段必须是 <code v-pre>「yes」</code> 、<code v-pre>「on」</code> 、<code v-pre>1</code>、<code v-pre>「1」</code>、<code v-pre>true</code> 或 <code v-pre>「true」</code>。这对于验证「服务条款」接受或类似字段非常有用。</p>
<h4 id="accepted-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#accepted-if-anotherfield-value"><span>accepted_if:anotherfield,value,...</span></a></h4>
<p>如果另一个验证中的字段等于指定值，则验证字段必须为 <code v-pre>「yes」</code> 、<code v-pre>「on」</code> 、<code v-pre>1</code>、<code v-pre>「1」</code>、<code v-pre>true</code> 或 <code v-pre>「true」</code>。这对于验证「服务条款」接受或类似字段非常有用。</p>
<h4 id="active-url" tabindex="-1"><a class="header-anchor" href="#active-url"><span>active_url</span></a></h4>
<p>根据 <code v-pre>dns_get_record</code> PHP函数，验证中的字段必须具有有效的 A 或 AAAA 记录。提供的URL的主机名在传递给 <code v-pre>dns_get_record</code> 之前使用 <code v-pre>parse_URL</code> PHP函数提取。</p>
<h4 id="after-date" tabindex="-1"><a class="header-anchor" href="#after-date"><span>after:<em>date</em></span></a></h4>
<p>验证中的字段必须是给定日期之后的值。日期将被传递到 <code v-pre>strtotime</code> PHP函数中，以便转换为有效的 <code v-pre>DateTime</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'start_date'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|date|after:tomorrow'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以指定另一个字段与日期进行比较，而不是传递要由 <code v-pre>strtotime</code> 计算的日期字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'finish_date'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|date|after:start_date'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="after-or-equal-date" tabindex="-1"><a class="header-anchor" href="#after-or-equal-date"><span>after_or_equal:<em>date</em></span></a></h4>
<p>待验证的字段必须是在给定日期之后或等于给定日期的值。有关详细信息，请参阅 <a href="#rule-after">after</a> 规则.</p>
<h4 id="alpha" tabindex="-1"><a class="header-anchor" href="#alpha"><span>alpha</span></a></h4>
<p>待验证的字段必须完全是包含在 <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AL%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{L}</code></a>和<a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AM%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{M}</code></a>中的Unicode字母字符。</p>
<p>为了将此验证规则限制在 ASCII 范围内的字符（<code v-pre>a-z</code> 和<code v-pre>A-Z</code>），你可以为验证规则提供 <code v-pre>ascii</code> 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'alpha:ascii'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="alpha-dash" tabindex="-1"><a class="header-anchor" href="#alpha-dash"><span>alpha_dash</span></a></h4>
<p>被验证的字段必须完全是 Unicode 字母数字字符中的 <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AL%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{L}</code></a>, <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AM%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{M}</code></a>, <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AN%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{N}</code></a>, 以及 ASCII 破折号（<code v-pre>-</code>）和 ASCII 下划线（<code v-pre>_</code>）。</p>
<p>为了将此验证规则限制在 ASCII 范围内的字符（<code v-pre>a-z</code> 和<code v-pre>A-Z</code>），你可以为验证规则提供 <code v-pre>ascii</code> 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'alpha_dash:ascii'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="alpha-num" tabindex="-1"><a class="header-anchor" href="#alpha-num"><span>alpha_num</span></a></h4>
<p>被验证的字段必须完全是 Unicode 字母数字字符中的 <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AL%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{L}</code></a>, <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AM%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{M}</code></a>, and <a href="https://util.unicode.org/UnicodeJsps/list-unicodeset.jsp?a=%5B%3AN%3A%5D&amp;g=&amp;i=" target="_blank" rel="noopener noreferrer"><code v-pre>\p{N}</code></a>.</p>
<p>为了将此验证规则限制在 ASCII 范围内的字符（<code v-pre>a-z</code> 和<code v-pre>A-Z</code>），你可以为验证规则提供 <code v-pre>ascii</code> 选项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'alpha_num:ascii'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="array" tabindex="-1"><a class="header-anchor" href="#array"><span>array</span></a></h4>
<p>待验证字段必须是有效的 PHP <code v-pre>数组</code>。</p>
<p>当向 <code v-pre>array</code> 规则提供附加值时，输入数组中的每个键都必须出现在提供给规则的值列表中。在以下示例中，输入数组中的 <code v-pre>admin</code> 键无效，因为它不包含在提供给 <code v-pre>array</code> 规则的值列表中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'taylorotwell'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'admin'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'array:name,username'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通常，你应该始终指定允许出现在数组中的数组键。</p>
<h4 id="ascii" tabindex="-1"><a class="header-anchor" href="#ascii"><span>ascii</span></a></h4>
<p>正在验证的字段必须完全是 7 位的 ASCII 字符。</p>
<h4 id="bail" tabindex="-1"><a class="header-anchor" href="#bail"><span>bail</span></a></h4>
<p>在首次验证失败后立即终止验证。</p>
<p>虽然 <code v-pre>bail</code> 规则只会在遇到验证失败时停止验证特定字段，但 <code v-pre>stopOnFirstFailure</code> 方法会通知验证器，一旦发生单个验证失败，它应该停止验证所有属性:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">stopOnFirstFailure</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">fails</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="before-date" tabindex="-1"><a class="header-anchor" href="#before-date"><span>before:<em>date</em></span></a></h4>
<p>待验证字段的值对应的日期必须在给定的日期之前。这个日期将被传递给 PHP 函数 <code v-pre>strtotime</code> 以便转化为有效的 <code v-pre>DateTime</code> 实例。此外，与 <a href="#rule-after"><code v-pre>after</code></a> 规则一致，可以将另外一个待验证的字段作为 <code v-pre>date</code> 的值。</p>
<h4 id="before-or-equal-date" tabindex="-1"><a class="header-anchor" href="#before-or-equal-date"><span>before_or_equal:<em>date</em></span></a></h4>
<p>待验证字段的值必须是给定日期之前或等于给定日期的值。这个日期将被传递给 PHP 函数 <code v-pre>strtotime</code> 以便转化为有效的 <code v-pre>DateTime</code> 实例。此外，与 <a href="#rule-after"><code v-pre>after</code></a> 规则一致， 可以将另外一个待验证的字段作为 <code v-pre>date</code> 的值。</p>
<h4 id="between-min-max" tabindex="-1"><a class="header-anchor" href="#between-min-max"><span>between:<em>min</em>,<em>max</em></span></a></h4>
<p>待验证字段值的大小必须介于给定的最小值和最大值（含）之间。字符串、数字、数组和文件的计算方式都使用 <a href="#rule-size"><code v-pre>size</code></a> 方法。</p>
<h4 id="boolean" tabindex="-1"><a class="header-anchor" href="#boolean"><span>boolean</span></a></h4>
<p>验证的字段必须能够被转换为布尔值。接受的输入包括 <code v-pre>true</code>, <code v-pre>false</code>, <code v-pre>1</code>, <code v-pre>0</code>, <code v-pre>「1」</code>, 和 <code v-pre>「0」</code>。</p>
<h4 id="confirmed" tabindex="-1"><a class="header-anchor" href="#confirmed"><span>confirmed</span></a></h4>
<p>验证的字段必须有一个匹配的 <code v-pre>{field}_confirmation</code> 字段。例如，如果正在验证的字段是 <code v-pre>password</code>，输入中必须存在一个匹配的 <code v-pre>password_confirmation</code> 字段。</p>
<h4 id="contains-foo-bar" tabindex="-1"><a class="header-anchor" href="#contains-foo-bar"><span>contains:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>这个验证字段必须是一个数组，其中包含了所有给定的参数数值。</p>
<h4 id="current-password" tabindex="-1"><a class="header-anchor" href="#current-password"><span>current_password</span></a></h4>
<p>验证的字段必须与认证用户的密码匹配。你可以使用规则的第一个参数指定一个 <a href="https://learnku.com/docs/laravel/11.x/authenticationmd" target="_blank" rel="noopener noreferrer">认证守卫</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'current_password:api'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="date" tabindex="-1"><a class="header-anchor" href="#date"><span>date</span></a></h4>
<p>验证字段必须是 <code v-pre>strtotime</code> PHP 函数可识别的有效日期。</p>
<h4 id="date-equals-date" tabindex="-1"><a class="header-anchor" href="#date-equals-date"><span>date_equals:<em>date</em></span></a></h4>
<p>验证字段必须等于给定日期。日期将传递到 PHP <code v-pre>strtotime</code> 函数中，以转换为有效的 <code v-pre>DateTime</code> 实例。</p>
<h4 id="date-format-format" tabindex="-1"><a class="header-anchor" href="#date-format-format"><span>date_format:<em>format</em>,...</span></a></h4>
<p>验证字段必须匹配给定的 <em>format</em> 。在验证字段时，您应该只使用 <code v-pre>date</code> 或 <code v-pre>date_format</code> 中的<strong>其中一个</strong>，而不是同时使用。此验证规则支持 PHP 的 <a href="https://www.php.net/manual/en/class.datetime.php" target="_blank" rel="noopener noreferrer">DateTime</a> 类支持的所有格式。</p>
<h4 id="decimal-min-max" tabindex="-1"><a class="header-anchor" href="#decimal-min-max"><span>decimal:<em>min</em>,<em>max</em></span></a></h4>
<p>验证的字段必须是数字并且必须包含指定数量的小数位：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 必须有两位小数 (9.99)...</span></span>
<span class="line"><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'decimal:2'</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 必须在2到4位小数之间...</span></span>
<span class="line"><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'decimal:2,4'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="declined" tabindex="-1"><a class="header-anchor" href="#declined"><span>declined</span></a></h4>
<p>正在验证的字段必须是 <code v-pre>「no」</code>，<code v-pre>「off」</code>，<code v-pre>0</code>，<code v-pre>「0」</code>，<code v-pre>false</code> 或者 <code v-pre>「false」</code> 。</p>
<h4 id="declined-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#declined-if-anotherfield-value"><span>declined_if:anotherfield,value,...</span></a></h4>
<p>如果另一个验证字段的值等于指定值，则验证字段的值必须为<code v-pre>「no」</code>，<code v-pre>「off」</code>，<code v-pre>0</code>，<code v-pre>「0」</code>，<code v-pre>false</code> 或者 <code v-pre>「false」</code> 。</p>
<h4 id="different-field" tabindex="-1"><a class="header-anchor" href="#different-field"><span>different:<em>field</em></span></a></h4>
<p>验证的字段必须与 <em>field</em> 有不同的值。</p>
<h4 id="digits-value" tabindex="-1"><a class="header-anchor" href="#digits-value"><span>digits:<em>value</em></span></a></h4>
<p>验证的整数必须具有确切长度 <em>value</em> 。</p>
<h4 id="digits-between-min-max" tabindex="-1"><a class="header-anchor" href="#digits-between-min-max"><span>digits_between:<em>min</em>,<em>max</em></span></a></h4>
<p>验证的整数长度必须在给定的 <em>min</em> 和 <em>max</em> 之间。</p>
<h4 id="dimensions" tabindex="-1"><a class="header-anchor" href="#dimensions"><span>dimensions</span></a></h4>
<p>文件在验证时，必须是符合特定尺寸要求的图像，这些要求通过验证规则的参数指定：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'avatar'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dimensions:min_width=100,min_height=200'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>可用的约束包括：<code v-pre>min_width</code>，<code v-pre>max_width</code>，<code v-pre>min_height</code>，<code v-pre>max_height</code>，<code v-pre>width</code>，<code v-pre>height</code>，<code v-pre>ratio</code>。</p>
<p><code v-pre>ratio</code> 约束应该表示为宽度除以高度。可以通过像 <code v-pre>3/2</code> 这样的分数或 <code v-pre>1.5</code> 这样的浮点数指定：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'avatar'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'dimensions:ratio=3/2'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>由于此规则需要多个参数，因此你可以使用 <code v-pre>Rule::dimensions</code> 方法来流畅地构建规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'avatar'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">dimensions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">maxWidth</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">maxHeight</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ratio</span><span class="token punctuation">(</span><span class="token number">3</span> <span class="token operator">/</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="distinct" tabindex="-1"><a class="header-anchor" href="#distinct"><span>distinct</span></a></h4>
<p>验证数组时，正在验证的字段不能有任何重复值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'foo.*.id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'distinct'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，Distinct 使用松散变量比较。要使用严格比较，你可以添加 <code v-pre>strict</code> 参数到你的验证规则定义中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'foo.*.id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'distinct:strict'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以在验证规则的参数中添加 <code v-pre>ignore_case</code>，以使规则忽略大小写差异：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'foo.*.id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'distinct:ignore_case'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="doesnt-start-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#doesnt-start-with-foo-bar"><span>doesnt_start_with:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>验证的字段不能以任何给定的值开始。</p>
<h4 id="doesnt-end-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#doesnt-end-with-foo-bar"><span>doesnt_end_with:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>验证的字段不能以任何给定的值结束。</p>
<h4 id="email" tabindex="-1"><a class="header-anchor" href="#email"><span>email</span></a></h4>
<p>在验证时，字段必须是格式化为电子邮件地址。这个验证规则使用 <a href="https://github.com/egulias/EmailValidator" target="_blank" rel="noopener noreferrer"><code v-pre>egulias/email-validator</code></a> 包来验证电子邮件地址。默认情况下，应用 <code v-pre>RFCValidation</code> 验证器，但你也可以应用其他验证样式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email:rfc,dns'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>上面的例子会应用 <code v-pre>RFCValidation</code> 和 <code v-pre>DNSCheckValidation</code> 验证。下面是你可以应用的一整套验证样式列表：</p>
<ul>
<li><code v-pre>rfc</code>: <code v-pre>RFCValidation</code></li>
<li><code v-pre>strict</code>: <code v-pre>NoRFCWarningsValidation</code></li>
<li><code v-pre>dns</code>: <code v-pre>DNSCheckValidation</code></li>
<li><code v-pre>spoof</code>: <code v-pre>SpoofCheckValidation</code></li>
<li><code v-pre>filter</code>: <code v-pre>FilterEmailValidation</code></li>
<li><code v-pre>filter_unicode</code>: <code v-pre>FilterEmailValidation::unicode()</code></li>
</ul>
<p><code v-pre>filter</code> 验证器使用 PHP 的 <code v-pre>filter_var</code> 函数，并在 Laravel 的 5.8 版本之前是 Laravel 默认的电子邮件验证行为。</p>
<blockquote>
<p><strong>注意</strong><br>
<code v-pre>dns</code> 和 <code v-pre>spoof</code> 验证器需要 PHP <code v-pre>intl</code> 扩展。</p>
</blockquote>
<h4 id="ends-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#ends-with-foo-bar"><span>ends_with:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>被验证的字段必须以给定值之一结尾。</p>
<h4 id="enum" tabindex="-1"><a class="header-anchor" href="#enum"><span>enum</span></a></h4>
<p><code v-pre>Enum</code> 规则是一个基于类的规则，验证字段是否包含有效的枚举值。<code v-pre>Enum</code> 规则接受枚举的名称作为其唯一的构造函数参数。在验证基本值时，应向<code v-pre>Enum</code> 规则提供一个后台枚举：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Enums<span class="token punctuation">\</span>ServerStatus</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'status'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">enum</span><span class="token punctuation">(</span><span class="token class-name static-context">ServerStatus</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>Enum</code> 规则的 <code v-pre>only</code> 和 <code v-pre>except</code> 方法可以用来限制哪些枚举情况应被视为有效：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">enum</span><span class="token punctuation">(</span><span class="token class-name static-context">ServerStatus</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token class-name static-context">ServerStatus</span><span class="token operator">::</span><span class="token constant">Pending</span><span class="token punctuation">,</span> <span class="token class-name static-context">ServerStatus</span><span class="token operator">::</span><span class="token constant">Active</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">enum</span><span class="token punctuation">(</span><span class="token class-name static-context">ServerStatus</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token class-name static-context">ServerStatus</span><span class="token operator">::</span><span class="token constant">Pending</span><span class="token punctuation">,</span> <span class="token class-name static-context">ServerStatus</span><span class="token operator">::</span><span class="token constant">Active</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>when</code> 方法可以用来有条件地修改 <code v-pre>Enum</code> 规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Auth</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">enum</span><span class="token punctuation">(</span><span class="token class-name static-context">ServerStatus</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">when</span><span class="token punctuation">(</span></span>
<span class="line">        <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$rule</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$rule</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token variable">$rule</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$rule</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="exclude" tabindex="-1"><a class="header-anchor" href="#exclude"><span>exclude</span></a></h4>
<p>在验证时，该字段将从 <code v-pre>validate</code> 和 <code v-pre>validated</code> 方法返回的请求数据中排除。</p>
<h4 id="exclude-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#exclude-if-anotherfield-value"><span>exclude_if:<em>anotherfield</em>,<em>value</em></span></a></h4>
<p>如果 <em>anotherfield</em> 字段等于 <em>value</em>，在验证时，该字段将从 <code v-pre>validate</code> 和 <code v-pre>validated</code> 方法返回的请求数据中排除。</p>
<p>如果需要复杂的条件排除逻辑，您可以使用 <code v-pre>Rule::excludeIf</code> 方法。此方法接受一个布尔值或一个闭包。提供闭包时，闭包应该返回 <code v-pre>true</code> 或 <code v-pre>false</code> 来指示是否应该排除正在验证的字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">excludeIf</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">excludeIf</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="exclude-unless-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#exclude-unless-anotherfield-value"><span>exclude_unless:<em>anotherfield</em>,<em>value</em></span></a></h4>
<p>除非 <em>anotherfield</em> 字段等于 <em>value</em>，否则在验证时，该字段将从 <code v-pre>validate</code> 和 <code v-pre>validated</code> 方法返回的请求数据中排除。如果 <em>value</em> 为 <code v-pre>null</code> (<code v-pre>exclude_unless:name,null</code>)，那么除非比较字段为 <code v-pre>null</code>，或比较字段在请求数据中丢失，否则将排除正在验证的字段。</p>
<h4 id="exclude-with-anotherfield" tabindex="-1"><a class="header-anchor" href="#exclude-with-anotherfield"><span>exclude_with:<em>anotherfield</em></span></a></h4>
<p>如果 <em>anotherfield</em> 字段存在，那么在验证时，该字段将从 <code v-pre>validate</code> 和 <code v-pre>validated</code> 方法返回的请求数据中排除。</p>
<h4 id="exclude-without-anotherfield" tabindex="-1"><a class="header-anchor" href="#exclude-without-anotherfield"><span>exclude_without:<em>anotherfield</em></span></a></h4>
<p>如果 <em>anotherfield</em> 字段不存在，那么在验证时，该字段将从 <code v-pre>validate</code> 和 <code v-pre>validated</code> 方法返回的请求数据中排除。</p>
<h4 id="exists-table-column" tabindex="-1"><a class="header-anchor" href="#exists-table-column"><span>exists:<em>table</em>,<em>column</em></span></a></h4>
<p>在验证时，该字段必须存在于给定的数据库表中。</p>
<h4 id="exists-规则的基本用法" tabindex="-1"><a class="header-anchor" href="#exists-规则的基本用法"><span>Exists 规则的基本用法</span></a></h4>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'state'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exists:states'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果没有指定 <code v-pre>column</code> 选项，将使用字段名。因此，在这种情况下，规则将验证 <code v-pre>states</code> 数据库表中是否包含一个记录，其 <code v-pre>state</code> 字段值与请求的 <code v-pre>state</code> 属性值匹配。</p>
<h4 id="指定自定义列名" tabindex="-1"><a class="header-anchor" href="#指定自定义列名"><span>指定自定义列名</span></a></h4>
<p>你可以明确指定应该由验证规则使用的数据库列名，方法是在数据库表名之后放置它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'state'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exists:states,abbreviation'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>有时，你可能需要指定一个特定的数据库连接来执行 <code v-pre>exists</code> 查询。你可以通过在表名前加上连接名来完成这个操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exists:connection.staff,email'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以指定应该用来确定表名的 Eloquent 模型，而不是直接指定表名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'user_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exists:App\Models\User,id'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你希望自定义验证规则执行的查询，你可以使用 <code v-pre>Rule</code> 类来流畅地定义规则。在这个例子中，我们还将把验证规则指定为数组，而不是使用 <code v-pre>|</code> 字符来分隔它们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Query<span class="token punctuation">\</span>Builder</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'staff'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Builder</span> <span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">return</span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'account_id'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过将列名作为第二个参数提供给 <code v-pre>Rule::exists</code> 方法，明确指定应该由 <code v-pre>exists</code> 规则使用的数据库列名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'state'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'states'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'abbreviation'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="extensions-foo-bar" tabindex="-1"><a class="header-anchor" href="#extensions-foo-bar"><span>extensions:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>验证的文件必须具有与列出的扩展名之一对应的用户分配的扩展名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'photo'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'extensions:jpg,png'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
你永远不应该仅依靠其用户分配的扩展名来验证文件。这个规则通常应该总是与 <a href="#rule-mimes"><code v-pre>mimes</code></a> 或 <a href="#rule-mimetypes"><code v-pre>mimetypes</code></a> 规则结合使用。</p>
</blockquote>
<h4 id="file" tabindex="-1"><a class="header-anchor" href="#file"><span>file</span></a></h4>
<p>在验证时，该字段必须是成功上传的文件。</p>
<h4 id="filled" tabindex="-1"><a class="header-anchor" href="#filled"><span>filled</span></a></h4>
<p>当字段存在时，它在验证时不得为空。</p>
<h4 id="gt-field" tabindex="-1"><a class="header-anchor" href="#gt-field"><span>gt:<em>field</em></span></a></h4>
<p>在验证时，字段必须大于给定的 <em>field</em> 或 <em>value</em>。两个字段必须是相同的类型。字符串、数字、数组和文件的评估使用与 <a href="#rule-size"><code v-pre>size</code></a> 规则相同的约定。</p>
<h4 id="gte-field" tabindex="-1"><a class="header-anchor" href="#gte-field"><span>gte:<em>field</em></span></a></h4>
<p>在验证时，字段必须大于或等于给定的 <em>field</em> 或 <em>value</em>。两个字段必须是相同的类型。字符串、数字、数组和文件的评估使用与 <a href="#rule-size"><code v-pre>size</code></a> 规则相同的约定。</p>
<h4 id="hex-color" tabindex="-1"><a class="header-anchor" href="#hex-color"><span>hex_color</span></a></h4>
<p>在验证时，字段必须包含有效的<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/hex-color" target="_blank" rel="noopener noreferrer">十六进制</a>格式的颜色值。</p>
<h4 id="image" tabindex="-1"><a class="header-anchor" href="#image"><span>image</span></a></h4>
<p>正在验证的文件必须是图像（jpg、jpeg、png、bmp、gif、svg 或 webp）。</p>
<h4 id="in-foo-bar" tabindex="-1"><a class="header-anchor" href="#in-foo-bar"><span>in:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>验证字段必须包含在给定的值列表中。由于此规则通常要求你 <code v-pre>implode</code> 数组，因此可以使用 <code v-pre>Rule::in</code> 方法来流畅地构造规则:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'zones'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">in</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'first-zone'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'second-zone'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>当 <code v-pre>in</code> 规则与 <code v-pre>array</code> 规则结合时，输入数组中的每个值都必须存在于提供给 <code v-pre>in</code> 规则的值列表中。在下面的例子中，输入数组中的 <code v-pre>LAS</code> 机场代码是无效的，因为它不包含在提供给 <code v-pre>in</code> 规则的机场列表中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'airports'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'NYC'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'LAS'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'airports'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'array'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'airports.*'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">in</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'NYC'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'LIT'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="in-array-anotherfield" tabindex="-1"><a class="header-anchor" href="#in-array-anotherfield"><span>in_array:<em>anotherfield</em>.*</span></a></h4>
<p>在验证时，字段必须存在于 <em>anotherfield</em> 的值中。</p>
<h4 id="integer" tabindex="-1"><a class="header-anchor" href="#integer"><span>integer</span></a></h4>
<p>在验证时，字段必须是整数。</p>
<blockquote>
<p><strong>注意</strong><br>
这个验证规则不验证输入是否是「integer」变量类型，而只是验证输入是否是 PHP 的 <code v-pre>FILTER_VALIDATE_INT</code> 规则接受的类型。如果您需要验证输入是否为数字，请将此规则与 <a href="#rule-numeric">numeric 验证规则</a> 结合使用。</p>
</blockquote>
<h4 id="ip" tabindex="-1"><a class="header-anchor" href="#ip"><span>ip</span></a></h4>
<p>在验证时，字段必须是 IP 地址。</p>
<h4 id="ipv4" tabindex="-1"><a class="header-anchor" href="#ipv4"><span>ipv4</span></a></h4>
<p>在验证时，字段必须是 IPv4 地址。</p>
<h4 id="ipv6" tabindex="-1"><a class="header-anchor" href="#ipv6"><span>ipv6</span></a></h4>
<p>在验证时，字段必须是 IPv6 地址。</p>
<h4 id="json" tabindex="-1"><a class="header-anchor" href="#json"><span>json</span></a></h4>
<p>在验证时，字段必须是有效的 JSON 字符串。</p>
<h4 id="lt-field" tabindex="-1"><a class="header-anchor" href="#lt-field"><span>lt:<em>field</em></span></a></h4>
<p>在验证时，字段必须小于给定 <em>field</em>。两个字段必须是相同类型。字符串、数字、数组和文件使用与 <a href="#rule-size"><code v-pre>size</code></a> 规则相同的规则评估。</p>
<h4 id="lte-field" tabindex="-1"><a class="header-anchor" href="#lte-field"><span>lte:<em>field</em></span></a></h4>
<p>在验证时，字段必须小于或等于给定 <em>field</em>。两个字段必须是相同类型。字符串、数字、数组和文件使用与 <a href="#rule-size"><code v-pre>size</code></a> 规则相同的规则评估。</p>
<h4 id="lowercase" tabindex="-1"><a class="header-anchor" href="#lowercase"><span>lowercase</span></a></h4>
<p>在验证时，字段必须是小写。</p>
<h4 id="list" tabindex="-1"><a class="header-anchor" href="#list"><span>list</span></a></h4>
<p>在验证时，字段必须是一个数组，并且是一个列表。如果数组的键由连续数字组成，从 0 到 <code v-pre>count($array) - 1</code>，则该数组被视为列表。</p>
<h4 id="mac-address" tabindex="-1"><a class="header-anchor" href="#mac-address"><span>mac_address</span></a></h4>
<p>在验证时，字段必须是 MAC 地址。</p>
<h4 id="max-value" tabindex="-1"><a class="header-anchor" href="#max-value"><span>max:<em>value</em></span></a></h4>
<p>在验证时，字段必须小于或等于最大 <em>value</em>。字符串、数字、数组和文件以与 <a href="#rule-size"><code v-pre>size</code></a> 规则相同的方式评估。</p>
<h4 id="max-digits-value" tabindex="-1"><a class="header-anchor" href="#max-digits-value"><span>max_digits:<em>value</em></span></a></h4>
<p>在验证时，整数必须有最大长度 <em>value</em>。</p>
<h4 id="mimetypes-text-plain" tabindex="-1"><a class="header-anchor" href="#mimetypes-text-plain"><span>mimetypes:<em>text/plain</em>,...</span></a></h4>
<p>在验证时，文件必须匹配给定的 MIME 类型之一：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'video'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mimetypes:video/avi,video/mpeg,video/quicktime'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>为了确定上传文件的 MIME 类型，文件的内容将被读取，框架将尝试猜测 MIME 类型，这可能与客户端提供的 MIME 类型不同。</p>
<h4 id="mimes-foo-bar" tabindex="-1"><a class="header-anchor" href="#mimes-foo-bar"><span>mimes:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>在验证时，文件必须有一个与列出的扩展名对应的 MIME 类型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'photo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'mimes:jpg,bmp,png'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>尽管您只需要指定扩展名，这个规则实际上通过阅读文件内容并猜测其 MIME 类型来验证文件的 MIME 类型。MIME 类型及其相应扩展的完整列表可以在以下位置找到：</p>
<p><a href="https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types" target="_blank" rel="noopener noreferrer">svn.apache.org/repos/asf/httpd/htt...</a></p>
<h4 id="mime-类型和扩展名" tabindex="-1"><a class="header-anchor" href="#mime-类型和扩展名"><span>MIME 类型和扩展名</span></a></h4>
<p>这个验证规则不验证文件的 MIME 类型和用户分配给文件的扩展名是否一致。例如，<code v-pre>mimes:png</code> 验证规则将认为包含有效 PNG 内容的文件是有效的 PNG 图片，即使该文件名为 <code v-pre>photo.txt</code>。如果你想验证文件的用户分配的扩展名，请使用 <a href="#rule-extensions"><code v-pre>extensions</code></a> 规则。</p>
<h4 id="min-value" tabindex="-1"><a class="header-anchor" href="#min-value"><span>min:<em>value</em></span></a></h4>
<p>在验证时，字段必须有一个最小 <em>value</em>。字符串、数字、数组和文件以与 <a href="#rule-size"><code v-pre>size</code></a> 规则相同的方式评估。</p>
<h4 id="min-digits-value" tabindex="-1"><a class="header-anchor" href="#min-digits-value"><span>min_digits:<em>value</em></span></a></h4>
<p>在验证时，整数必须有最小长度 <em>value</em>。</p>
<h4 id="multiple-of-value" tabindex="-1"><a class="header-anchor" href="#multiple-of-value"><span>multiple_of:<em>value</em></span></a></h4>
<p>在验证时，字段必须是 <em>value</em> 的倍数。</p>
<h4 id="missing" tabindex="-1"><a class="header-anchor" href="#missing"><span>missing</span></a></h4>
<p>在验证时，字段必须不在输入数据中出现。</p>
<h4 id="missing-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#missing-if-anotherfield-value"><span>missing_if:<em>anotherfield</em>,<em>value</em>,...</span></a></h4>
<p>如果 <em>anotherfield</em> 字段等于任何 <em>value</em>，那么在验证时，字段必须不出现在输入中。</p>
<h4 id="missing-unless-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#missing-unless-anotherfield-value"><span>missing_unless:<em>anotherfield</em>,<em>value</em></span></a></h4>
<p>除非 <em>anotherfield</em> 字段等于任何 <em>value</em>，否则在验证时，字段必须不出现在输入中。</p>
<h4 id="missing-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#missing-with-foo-bar"><span>missing_with:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>在验证时，只有当任何其他指定字段出现时，字段必须不出现在输入中。</p>
<h4 id="missing-with-all-foo-bar" tabindex="-1"><a class="header-anchor" href="#missing-with-all-foo-bar"><span>missing_with_all:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>在验证时，只有当所有其他指定字段都出现时，字段必须不出现在输入中。</p>
<h4 id="not-in-foo-bar" tabindex="-1"><a class="header-anchor" href="#not-in-foo-bar"><span>not_in:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>在验证时，字段不能包括在给定值列表中。可以使用 <code v-pre>Rule::notIn</code> 方法流畅地构建规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'toppings'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">notIn</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'sprinkles'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'cherries'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="not-regex-pattern" tabindex="-1"><a class="header-anchor" href="#not-regex-pattern"><span>not_regex:<em>pattern</em></span></a></h4>
<p>在验证时，字段不能匹配给定的正则表达式。</p>
<p>在内部，这个规则使用 PHP <code v-pre>preg_match</code> 函数。指定的模式应该遵守 <code v-pre>preg_match</code> 所要求的相同格式，因此也应该包括有效的分隔符。例如：<code v-pre>'email' =&gt; 'not_regex:/^.+$/i'</code>。</p>
<blockquote>
<p><strong>注意</strong><br>
使用 <code v-pre>regex</code> / <code v-pre>not_regex</code> 模式时，可能需要使用数组来指定验证规则，而不是使用 <code v-pre>|</code> 分隔符，尤其是当正则表达式包含 <code v-pre>|</code> 字符时。</p>
</blockquote>
<h4 id="nullable" tabindex="-1"><a class="header-anchor" href="#nullable"><span>nullable</span></a></h4>
<p>在验证时，该字段可为 <code v-pre>null</code>。</p>
<h4 id="numeric" tabindex="-1"><a class="header-anchor" href="#numeric"><span>numeric</span></a></h4>
<p>在验证时，字段必须是<a href="https://www.php.net/manual/en/function.is-numeric.php" target="_blank" rel="noopener noreferrer">数值类型</a>。</p>
<h4 id="present" tabindex="-1"><a class="header-anchor" href="#present"><span>present</span></a></h4>
<p>在验证时，字段必须存在于输入数据中。</p>
<h4 id="present-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#present-if-anotherfield-value"><span>present_if:<em>anotherfield</em>,<em>value</em>,...</span></a></h4>
<p>当 <em>anotherfield</em> 字段等于任何 <em>value</em> 时，在验证时，该字段必须存在。</p>
<h4 id="present-unless-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#present-unless-anotherfield-value"><span>present_unless:<em>anotherfield</em>,<em>value</em></span></a></h4>
<p>除非 <em>anotherfield</em> 字段等于任何 <em>value</em>，在验证时，该字段必须存在。</p>
<h4 id="present-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#present-with-foo-bar"><span>present_with:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p><em>只有在</em> 任何其他指定字段存在且不为空时，在验证时，该字段必须存在且不为空。</p>
<h4 id="present-with-all-foo-bar" tabindex="-1"><a class="header-anchor" href="#present-with-all-foo-bar"><span>present_with_all:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p><em>只有在</em> 所有其他指定字段都存在且不为空时，在验证时，该字段必须存在且不为空。</p>
<h4 id="prohibited" tabindex="-1"><a class="header-anchor" href="#prohibited"><span>prohibited</span></a></h4>
<p>在验证时，该字段必须缺失或空。字段为「空」的情况如下：</p>
<ul>
<li>值为 <code v-pre>null</code>。</li>
<li>值为空字符串。</li>
<li>值为空数组或空的 <code v-pre>Countable</code> 对象。</li>
<li>值为路径为空的上传文件。</li>
</ul>
<h4 id="prohibited-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#prohibited-if-anotherfield-value"><span>prohibited_if:<em>anotherfield</em>,<em>value</em>,...</span></a></h4>
<p>当 <em>anotherfield</em> 字段等于任何 <em>value</em> 时，在验证时，该字段必须缺失或空。字段为「空」的情况如下：</p>
<ul>
<li>值为 <code v-pre>null</code>。</li>
<li>值为空字符串。</li>
<li>值为空数组或空的 <code v-pre>Countable</code> 对象。</li>
<li>值为路径为空的上传文件。</li>
</ul>
<p>如果需要复杂的条件禁用逻辑，可以使用 <code v-pre>Rule::prohibitedIf</code> 方法。此方法接受一个布尔值或闭包。使用闭包时，闭包应该返回 <code v-pre>true</code> 或 <code v-pre>false</code>，以指示是否禁止该字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">prohibitedIf</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">prohibitedIf</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="prohibited-unless-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#prohibited-unless-anotherfield-value"><span>prohibited_unless:<em>anotherfield</em>,<em>value</em>,...</span></a></h4>
<p>除非 <em>anotherfield</em> 字段等于任何 <em>value</em>，在验证时，该字段必须缺失或空。字段为「空」的情况如下：</p>
<ul>
<li>值为 <code v-pre>null</code>。</li>
<li>值为空字符串。</li>
<li>值为空数组或空的 <code v-pre>Countable</code> 对象。</li>
<li>值为路径为空的上传文件。</li>
</ul>
<h4 id="prohibits-anotherfield" tabindex="-1"><a class="header-anchor" href="#prohibits-anotherfield"><span>prohibits:<em>anotherfield</em>,...</span></a></h4>
<p>如果验证字段未缺失或非空，则 <em>anotherfield</em> 中的所有字段必须缺失或空。字段为「空」的情况如下：</p>
<ul>
<li>值为 <code v-pre>null</code>。</li>
<li>值为空字符串。</li>
<li>值为空数组或空的 <code v-pre>Countable</code> 对象。</li>
<li>值为路径为空的上传文件。</li>
</ul>
<h4 id="regex-pattern" tabindex="-1"><a class="header-anchor" href="#regex-pattern"><span>regex:<em>pattern</em></span></a></h4>
<p>在验证时，字段必须匹配给定的正则表达式。</p>
<p>在内部，此规则使用 PHP 的 <code v-pre>preg_match</code> 函数。指定的模式应遵循 <code v-pre>preg_match</code> 所要求的格式，并包含有效的分隔符。例如：<code v-pre>'email' =&gt; 'regex:/^.+@.+$/i'</code>。</p>
<blockquote>
<p><strong>注意</strong><br>
当使用 <code v-pre>regex</code> / <code v-pre>not_regex</code> 模式时，可能需要使用数组而不是使用 <code v-pre>|</code> 分隔符来指定规则，尤其是当正则表达式包含 <code v-pre>|</code> 字符时。</p>
</blockquote>
<h4 id="required" tabindex="-1"><a class="header-anchor" href="#required"><span>required</span></a></h4>
<p>在验证时，字段必须存在于输入数据中且不为空。字段为「空」的情况如下：</p>
<ul>
<li>值为 <code v-pre>null</code>。</li>
<li>值为空字符串。</li>
<li>值为空数组或空的 <code v-pre>Countable</code> 对象。</li>
<li>无路径的上传文件。</li>
</ul>
<h4 id="required-if-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#required-if-anotherfield-value"><span>required_if:<em>anotherfield</em>,<em>value</em>,...</span></a></h4>
<p>当 <em>anotherfield</em> 字段等于任何 <em>value</em> 时，在验证时，字段必须存在且不为空。</p>
<p>如果需要构建 <code v-pre>required_if</code> 规则的更复杂条件，你可以使用 <code v-pre>Rule::requiredIf</code> 方法。此方法接受一个布尔值或闭包。当传递闭包时，闭包应返回 <code v-pre>true</code> 或 <code v-pre>false</code> 以指示验证的字段是不是必须的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">requiredIf</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'role_id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">requiredIf</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$request</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">is_admin</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="required-if-accepted-anotherfield" tabindex="-1"><a class="header-anchor" href="#required-if-accepted-anotherfield"><span>required_if_accepted:<em>anotherfield</em>,...</span></a></h4>
<p>如果 <em>anotherfield</em> 字段等于 <code v-pre>「yes」</code>, <code v-pre>「on」</code>, <code v-pre>1</code>, <code v-pre>「1」</code>, <code v-pre>true</code>, 或 <code v-pre>「true」</code>，在验证时，该字段必须存在且不为空。</p>
<h4 id="required-if-declined-anotherfield" tabindex="-1"><a class="header-anchor" href="#required-if-declined-anotherfield"><span>required_if_declined:<em>anotherfield</em>,...</span></a></h4>
<p>如果 <em>anotherfield</em> 字段等于 <code v-pre>「no」</code>, <code v-pre>「off」</code>, <code v-pre>0</code>, <code v-pre>「0」</code>, <code v-pre>false</code>, 或 <code v-pre>「false」</code>，在验证时，该字段必须存在且不为空。</p>
<h4 id="required-unless-anotherfield-value" tabindex="-1"><a class="header-anchor" href="#required-unless-anotherfield-value"><span>required_unless:<em>anotherfield</em>,<em>value</em>,...</span></a></h4>
<p>除非 <em>anotherfield</em> 字段等于任何 <em>value</em>，在验证时，字段必须存在且不为空。这也意味着 <em>anotherfield</em> 必须存在于请求数据中，除非 <em>value</em> 为 <code v-pre>null</code>。如果 <em>value</em> 为 <code v-pre>null</code> (<code v-pre>required_unless:name,null</code>)，除非比较字段为 <code v-pre>null</code> 或者比较字段丢失于请求数据，否则验证的字段是必需的。</p>
<h4 id="required-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-with-foo-bar"><span>required_with:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p><em>只有在</em> 任何其他指定字段存在且不为空时，在验证时，该字段必须存在且不为空。</p>
<h4 id="required-with-all-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-with-all-foo-bar"><span>required_with_all:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p><em>只有在</em> 所有其他指定字段都存在且不为空时，在验证时，该字段必须存在且不为空。</p>
<h4 id="required-without-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-without-foo-bar"><span>required_without:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p><em>只有在</em> 任何其他指定字段为空或不存在时，在验证时，该字段必须存在且不为空。</p>
<h4 id="required-without-all-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-without-all-foo-bar"><span>required_without_all:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p><em>只有在</em> 所有其他指定字段为空或不存在时，在验证时，该字段必须存在且不为空。</p>
<h4 id="required-array-keys-foo-bar" tabindex="-1"><a class="header-anchor" href="#required-array-keys-foo-bar"><span>required_array_keys:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>在验证时，字段必须是一个数组并且至少包含指定的键。</p>
<h4 id="same-field" tabindex="-1"><a class="header-anchor" href="#same-field"><span>same:<em>field</em></span></a></h4>
<p>在验证时，给定的 <em>field</em> 必须与验证字段匹配。</p>
<h4 id="size-value" tabindex="-1"><a class="header-anchor" href="#size-value"><span>size:<em>value</em></span></a></h4>
<p>在验证时，字段的大小必须匹配给定的 <em>value</em>。对于字符串数据，<em>value</em> 对应字符数。对于数值数据，<em>value</em> 对应一个给定的整数值（属性也必须有 <code v-pre>numeric</code> 或 <code v-pre>integer</code> 规则）。对于数组，<em>size</em> 对应数组的 <code v-pre>count</code>。对于文件，<em>size</em> 对应文件的大</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 验证一个字符串是否恰好是 12 个字符长..</span></span>
<span class="line"><span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'size:12'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 验证一个提供的整数等于 10...</span></span>
<span class="line"><span class="token string single-quoted-string">'seats'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'integer|size:10'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 验证一个数组是否恰好有 5 个元素...</span></span>
<span class="line"><span class="token string single-quoted-string">'tags'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'array|size:5'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 验证一个上传的文件是否恰好是 512 千字节...</span></span>
<span class="line"><span class="token string single-quoted-string">'image'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'file|size:512'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="starts-with-foo-bar" tabindex="-1"><a class="header-anchor" href="#starts-with-foo-bar"><span>starts_with:<em>foo</em>,<em>bar</em>,...</span></a></h4>
<p>在验证时，字段必须以给定值中的一个开始。</p>
<h4 id="string" tabindex="-1"><a class="header-anchor" href="#string"><span>string</span></a></h4>
<p>在验证时，字段必须是一个字符串。如果你也想允许字段为 <code v-pre>null</code>，应将 <code v-pre>nullable</code> 规则分配给该字段。</p>
<h4 id="timezone" tabindex="-1"><a class="header-anchor" href="#timezone"><span>timezone</span></a></h4>
<p>在验证时，字段必须是 <code v-pre>DateTimeZone::listIdentifiers</code> 方法根据的一个有效的时区标识符。</p>
<p>也可以将 <a href="https://www.php.net/manual/en/datetimezone.listidentifiers.php" target="_blank" rel="noopener noreferrer"><code v-pre>DateTimeZone::listIdentifiers</code> 方法接受的参数</a> 提供给这个验证规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'timezone'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|timezone:all'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token string single-quoted-string">'timezone'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|timezone:Africa'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token string single-quoted-string">'timezone'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|timezone:per_country,US'</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="unique-table-column" tabindex="-1"><a class="header-anchor" href="#unique-table-column"><span>unique:<em>table</em>,<em>column</em></span></a></h4>
<p>在验证时，该字段在给定数据库表中必须不存在。</p>
<p>*<em>指定自定义表格 / 列名：</em></p>
<p>你可以指定 Eloquent 模型来确定表名，而不是直接指定表名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'unique:App\Models\User,email_address'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>column</code> 选项用于指定字段对应的数据库列。如果没有指定 <code v-pre>column</code> 选项，则会使用验证字段的名称。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'unique:users,email_address'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><strong>指定自定义数据库连接</strong></p>
<p>偶尔，你可能需要为 Validator 执行的数据库查询设置自定义连接。为此，你可以在表名前加上连接名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'unique:connection.users,email_address'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><strong>强制唯一规则忽略给定的 ID:</strong></p>
<p>有时，你可能希望在唯一性验证中忽略给定的 ID。例如，考虑一个包括用户姓名、电子邮件地址和位置的「更新个人信息」界面。你可能希望验证电子邮件地址的唯一性。但是，如果用户仅更改姓名字段而不是电子邮件字段，则你不希望因用户已经是该电子邮件地址的所有者而抛出验证错误。</p>
<p>为指导验证器忽略用户的 ID，我们将使用 <code v-pre>Rule</code> 类来流畅定义规则。在这个例子中，我们还将以数组形式指定验证规则，而不是使用 <code v-pre>|</code> 字符来界定规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
你不应将任何用户控制的请求输入传递到 <code v-pre>ignore</code> 方法中。相反，你应该只传递系统生成的唯一 ID，如 Eloquent 模型实例的自增 ID 或 UUID。否则，你的应用程序将容易受到 SQL 注入攻击。</p>
</blockquote>
<p>你可以将整个模型实例而不是模型键的值传递给 <code v-pre>ignore</code> 方法。Laravel 将自动从模型中提取键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果你的表使用的主键列名不是 <code v-pre>id</code>，你可以在调用 <code v-pre>ignore</code> 方法时指定该列的名称：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'user_id'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，<code v-pre>unique</code> 规则将检查与正在验证的属性名称相匹配的列的唯一性。但是，你可以将不同的列名作为第二个参数传递给 <code v-pre>unique</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email_address'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ignore</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><strong>添加额外的查询条件：</strong></p>
<p>你可以通过使用 <code v-pre>where</code> 方法自定义查询来指定额外的查询条件。例如，让我们添加一个查询条件，将查询限定在只搜索 <code v-pre>account_id</code> 列值为 <code v-pre>1</code> 的记录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Builder</span> <span class="token variable">$query</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$query</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'account_id'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="uppercase" tabindex="-1"><a class="header-anchor" href="#uppercase"><span>uppercase</span></a></h4>
<p>在验证时，该字段必须为大写。</p>
<h4 id="url" tabindex="-1"><a class="header-anchor" href="#url"><span>url</span></a></h4>
<p>在验证时，该字段必须是有效的 URL。</p>
<p>如果你想指定应该被视为有效的 URL 协议，你可以将协议作为验证规则参数传递：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'url:http,https'</span><span class="token punctuation">,</span></span>
<span class="line"></span>
<span class="line"><span class="token string single-quoted-string">'game'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'url:minecraft,steam'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ulid" tabindex="-1"><a class="header-anchor" href="#ulid"><span>ulid</span></a></h4>
<p>在验证时，该字段必须是有效的 <a href="https://github.com/ulid/spec" target="_blank" rel="noopener noreferrer">通用唯一字典排序标识符</a> (ULID)。</p>
<h4 id="uuid" tabindex="-1"><a class="header-anchor" href="#uuid"><span>uuid</span></a></h4>
<p>在验证时，该字段必须是一个有效的 RFC 4122 (版本 1, 3, 4, 或 5) 通用唯一标识符 (UUID)。</p>
<h2 id="根据条件添加规则" tabindex="-1"><a class="header-anchor" href="#根据条件添加规则"><span>根据条件添加规则</span></a></h2>
<h4 id="跳过验证某些字段的值时" tabindex="-1"><a class="header-anchor" href="#跳过验证某些字段的值时"><span>跳过验证某些字段的值时</span></a></h4>
<p>有时你可能想在另一个字段具有给定值时不验证给定的字段。你可以使用 <code v-pre>exclude_if</code> 验证规则来实现这一点。在此示例中，如果 <code v-pre>has_appointment</code> 字段的值为 <code v-pre>false</code>，则不会验证 <code v-pre>appointment_date</code> 和 <code v-pre>doctor_name</code> 字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'has_appointment'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|boolean'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'appointment_date'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exclude_if:has_appointment,false|required|date'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'doctor_name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exclude_if:has_appointment,false|required|string'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，你可以使用 <code v-pre>exclude_unless</code> 规则在另一个字段具有给定值时验证给定字段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'has_appointment'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|boolean'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'appointment_date'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exclude_unless:has_appointment,true|required|date'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'doctor_name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'exclude_unless:has_appointment,true|required|string'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="在字段存在时验证" tabindex="-1"><a class="header-anchor" href="#在字段存在时验证"><span>在字段存在时验证</span></a></h4>
<p>在某些情况下，你可能希望 <strong>仅当</strong> 该字段存在于被验证的数据中时才对字段进行验证检查。要快速实现这一点，请将 <code v-pre>sometimes</code> 规则添加到您的规则列表中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$v</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'sometimes|required|email'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在上面的例子中，如果 <code v-pre>email</code> 字段存在于 <code v-pre>$data</code> 数组中，则只会验证该字段。</p>
<blockquote>
<p><strong>注意</strong><br>
如果你正在尝试验证应始终存在但可能为空的字段，请查看<a href="#a-note-on-optional-fields">可选字段的说明</a>。</p>
</blockquote>
<h4 id="复杂条件验证" tabindex="-1"><a class="header-anchor" href="#复杂条件验证"><span>复杂条件验证</span></a></h4>
<p>有时你可能希望基于更复杂的条件逻辑添加验证规则。例如，如果另一个字段的值大于 100，你可能希望要求给定字段。或者，你可能需要两个字段在另一个字段存在时具有给定值。添加这些验证规则不必痛苦。首先，用您的 <em>静态规则</em> 创建一个 <code v-pre>Validator</code> 实例，这些规则永远不会改变：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|email'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'games'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|numeric'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>让我们假设我们的网站应用程序是为游戏收藏家设计的。如果游戏收藏家向我们的应用程序注册并拥有超过 100 个游戏，我们希望他们解释为什么拥有这么多游戏。例如，可能他们经营游戏转售店，或者他们只是喜欢收集游戏。要有条件地添加此要求，我们可以使用 <code v-pre>Validator</code> 实例上的 <code v-pre>sometimes</code> 方法。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Fluent</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">sometimes</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'reason'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'required|max:500'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Fluent</span> <span class="token variable">$input</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$input</span><span class="token operator">-></span><span class="token property">games</span> <span class="token operator">>=</span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>传递给 <code v-pre>sometimes</code> 方法的第一个参数是我们正在有条件地验证的字段的名称。第二个参数是我们想要添加的规则列表。如果第三个参数传递的闭包返回 <code v-pre>true</code>，则将添加规则。此方法可以轻松构建复杂的条件验证。你甚至可以一次为多个字段添加条件性验证：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">sometimes</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'reason'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'cost'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Fluent</span> <span class="token variable">$input</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$input</span><span class="token operator">-></span><span class="token property">games</span> <span class="token operator">>=</span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
传递给闭包的 <code v-pre>$input</code> 参数将是 <code v-pre>Illuminate\Support\Fluent</code> 的实例，可用于访问您正在验证的输入和文件。</p>
</blockquote>
<h4 id="复杂条件数组验证" tabindex="-1"><a class="header-anchor" href="#复杂条件数组验证"><span>复杂条件数组验证</span></a></h4>
<p>有时你可能希望根据同一个嵌套数组中的另一个字段验证一个字段，而你不知道该字段的索引。在这些情况下，你可以让您的闭包接收第二个参数，这将是数组中当前正在验证的单个项目：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'channels'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'type'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'address'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'abigail@example.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'type'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'url'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'address'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'https://example.com'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">sometimes</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'channels.*.address'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Fluent</span> <span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Fluent</span> <span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$item</span><span class="token operator">-></span><span class="token property">type</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span><span class="token operator">-></span><span class="token function">sometimes</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'channels.*.address'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'url'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Fluent</span> <span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Fluent</span> <span class="token variable">$item</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$item</span><span class="token operator">-></span><span class="token property">type</span> <span class="token operator">!==</span> <span class="token string single-quoted-string">'email'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>与传递给闭包的 <code v-pre>$input</code> 参数一样，当属性数据是数组时，<code v-pre>$item</code> 参数是 <code v-pre>Illuminate\Support\Fluent</code> 的实例；否则，它是一个字符串。</p>
<h2 id="验证数组" tabindex="-1"><a class="header-anchor" href="#验证数组"><span>验证数组</span></a></h2>
<p>如<a href="#rule-array"><code v-pre>array</code> 验证规则文档</a>中所讨论，<code v-pre>array</code> 规则接受允许的数组键列表。如果数组中存在任何其他键，验证将失败：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor Otwell'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'username'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'taylorotwell'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'admin'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'array:name,username'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通常，你应始终指定允许出现在你数组中的键。否则，验证器的 <code v-pre>validate</code> 和 <code v-pre>validated</code> 方法将返回所有经过验证的数据，包括数组及其所有键，即使这些键未通过其他嵌套数组验证规则进行验证。</p>
<h3 id="验证嵌套数组输入" tabindex="-1"><a class="header-anchor" href="#验证嵌套数组输入"><span>验证嵌套数组输入</span></a></h3>
<p>验证基于嵌套数组的表单输入字段不必繁琐。您可以使用「.」语法来验证数组内的属性。例如，如果传入的 HTTP 请求包含 <code v-pre>photos[profile]</code> 字段，你可以这样验证它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'photos.profile'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required|image'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以验证数组的每个元素。例如，要验证给定数组输入字段中的每个电子邮件是否唯一，您可以执行以下操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'person.*.email'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'email|unique:users'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'person.*.first_name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required_with:person.*.last_name'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>同样，你可以在为基于数组的字段指定<a href="#custom-messages-for-specific-attributes">自定义验证消息</a>时使用 <code v-pre>*</code> 字符，使得为基于数组的字段使用单个验证消息变得轻而易举：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'custom'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'person.*.email'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'unique'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Each person must have a unique email address'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="访问嵌套数组数据" tabindex="-1"><a class="header-anchor" href="#访问嵌套数组数据"><span>访问嵌套数组数据</span></a></h4>
<p>有时你可能需要在为属性分配验证规则时访问给定嵌套数组元素的值。你可以使用 <code v-pre>Rule::forEach</code> 方法来实现这一点。<code v-pre>forEach</code> 方法接受一个闭包，该闭包将在验证的数组属性的每次迭代中被调用，并接收属性的值和明确的、完全展开的属性名称。闭包应返回分配给数组元素的一组规则：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>HasPermission</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'companies.*.id'</span> <span class="token operator">=></span> <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">forEach</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-declaration">string</span><span class="token operator">|</span><span class="token keyword type-declaration">null</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$attribute</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token punctuation">[</span></span>
<span class="line">            <span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token class-name static-context">Company</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'id'</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token keyword">new</span> <span class="token class-name">HasPermission</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'manage-company'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="错误消息索引和位置" tabindex="-1"><a class="header-anchor" href="#错误消息索引和位置"><span>错误消息索引和位置</span></a></h3>
<p>在验证数组时，你可能希望在错误消息中引用验证失败的特定项的索引或位置。为了实现这一点，你可以在<a href="#manual-customizing-the-error-messages">自定义验证消息</a>中包含 <code v-pre>:index</code>（从 <code v-pre>0</code> 开始）和 <code v-pre>:position</code>（从 <code v-pre>1</code> 开始）占位符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'photos'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'BeachVacation.jpg'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'description'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'我海滩度假的照片！'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span></span>
<span class="line">            <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'GrandCanyon.jpg'</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token string single-quoted-string">'description'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'photos.*.description'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'photos.*.description.required'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'请描述第 :position 张照片。'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>根据上述示例，验证将失败，用户将收到以下错误信息：「请描述第 2 张照片。.」</p>
<p>如有必要，你可以通过 <code v-pre>second-index</code>、<code v-pre>second-position</code>、<code v-pre>third-index</code>、<code v-pre>third-position</code> 等引用更深层次嵌套索引和位置。</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'photos.*.attributes.*.string'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Invalid attribute for photo #:second-position.'</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="验证文件" tabindex="-1"><a class="header-anchor" href="#验证文件"><span>验证文件</span></a></h2>
<p>Laravel 提供了多种验证规则，可用于验证上传的文件，例如 <code v-pre>mimes</code>、<code v-pre>image</code>、<code v-pre>min</code> 和 <code v-pre>max</code>。虽然你可以在验证文件时单独指定这些规则，Laravel 还提供了一个灵活的文件验证规则构建器，你可能会觉得它很方便：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>File</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'attachment'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">File</span><span class="token operator">::</span><span class="token function">types</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'mp3'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'wav'</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">1024</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">max</span><span class="token punctuation">(</span><span class="token number">12</span> <span class="token operator">*</span> <span class="token number">1024</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你的应用程序接受用户上传的图像，你可以使用 <code v-pre>File</code> 规则的 <code v-pre>image</code> 构造函数方法来表明上传的文件应该是一张图像。此外，<code v-pre>dimensions</code> 规则可用于限制图像的尺寸：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rule</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>File</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'photo'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token class-name static-context">File</span><span class="token operator">::</span><span class="token function">image</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">1024</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">max</span><span class="token punctuation">(</span><span class="token number">12</span> <span class="token operator">*</span> <span class="token number">1024</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">dimensions</span><span class="token punctuation">(</span><span class="token class-name static-context">Rule</span><span class="token operator">::</span><span class="token function">dimensions</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">maxWidth</span><span class="token punctuation">(</span><span class="token number">1000</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">maxHeight</span><span class="token punctuation">(</span><span class="token number">500</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
有关验证图像尺寸的更多信息，请参阅<a href="#rule-dimensions">尺寸规则文档</a>。</p>
</blockquote>
<h4 id="文件大小" tabindex="-1"><a class="header-anchor" href="#文件大小"><span>文件大小</span></a></h4>
<p>为了方便，最小和最大文件大小可以指定为带有表示文件大小单位的后缀的字符串。支持 <code v-pre>kb</code>、<code v-pre>mb</code>、<code v-pre>gb</code> 和 <code v-pre>tb</code> 后缀：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">File</span><span class="token operator">::</span><span class="token function">image</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">min</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'1kb'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">max</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'10mb'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="文件类型" tabindex="-1"><a class="header-anchor" href="#文件类型"><span>文件类型</span></a></h4>
<p>尽管调用 <code v-pre>types</code> 方法时你只需要指定扩展名，这个方法实际上通过读取文件的内容和猜测其 MIME 类型来验证文件的 MIME 类型。MIME 类型及其对应扩展的完整列表可在以下位置找到：</p>
<p><a href="https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types" target="_blank" rel="noopener noreferrer">svn.apache.org/repos/asf/httpd/htt...</a></p>
<h2 id="验证密码" tabindex="-1"><a class="header-anchor" href="#验证密码"><span>验证密码</span></a></h2>
<p>为确保密码具有足够的复杂性，你可以使用 Laravel 的 <code v-pre>Password</code> 规则对象：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>Password</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'confirmed'</span><span class="token punctuation">,</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>Password</code> 规则对象允许你轻松自定义应用程序的密码复杂性要求，比如指定密码需要至少一个字母、数字、符号或大小写混合字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 要求至少 8 个字符..</span></span>
<span class="line"><span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 要求至少一个字母...</span></span>
<span class="line"><span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">letters</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 要求至少一个大写和小写字母...</span></span>
<span class="line"><span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mixedCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 要求至少一个数字...</span></span>
<span class="line"><span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">numbers</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 要求至少一个符号...</span></span>
<span class="line"><span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">symbols</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>此外，你可以使用 <code v-pre>uncompromised</code> 方法确保密码在公开的密码数据泄露中未被泄露：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">uncompromised</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>在内部，<code v-pre>Password</code> 规则对象使用 <a href="https://en.wikipedia.org/wiki/K-anonymity" target="_blank" rel="noopener noreferrer">k-Anonymity</a> 模型通过 <a href="https://haveibeenpwned.com/" target="_blank" rel="noopener noreferrer">haveibeenpwned.com</a> 服务确定密码是否已泄露，而不损害用户的隐私或安全。</p>
<p>默认情况下，如果密码在数据泄漏中出现至少一次，则将被认为已经泄露。你可以使用 <code v-pre>uncompromised</code> 方法的第一个参数自定义此阈值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 确保密码在相同数据泄露中出现少于 3 次...</span></span>
<span class="line"><span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">uncompromised</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div></div></div><p>当然，你可以链式调用上述所有方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">letters</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">mixedCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">numbers</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">symbols</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">uncompromised</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="定义默认密码规则" tabindex="-1"><a class="header-anchor" href="#定义默认密码规则"><span>定义默认密码规则</span></a></h4>
<p>你可能会发现在应用程序的单个位置指定密码的默认验证规则很方便。你可以使用 <code v-pre>Password::defaults</code> 方法轻松完成这项工作，该方法接受一个闭包。<code v-pre>defaults</code> 方法给出的闭包应返回密码规则的默认配置。通常，<code v-pre>defaults</code> 规则应在你的应用程序的服务提供者之一的 <code v-pre>boot</code> 方法中调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>Password</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token doc-comment comment">/**</span>
<span class="line"> * 引导任何应用程序服务</span>
<span class="line"> */</span></span>
<span class="line"><span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">boot</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">defaults</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$rule</span> <span class="token operator">=</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">app</span><span class="token operator">-></span><span class="token function">isProduction</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token operator">?</span> <span class="token variable">$rule</span><span class="token operator">-></span><span class="token function">mixedCase</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">uncompromised</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line">                    <span class="token punctuation">:</span> <span class="token variable">$rule</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>然后，当你希望将默认规则应用于正在验证的特定密码时，可以不带参数地调用 <code v-pre>defaults</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token string single-quoted-string">'password'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">defaults</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>有时，你可能希望将额外的验证规则附加到默认的密码验证规则。你可以使用 <code v-pre>rules</code> 方法完成此操作：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>ZxcvbnRule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">defaults</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$rule</span> <span class="token operator">=</span> <span class="token class-name static-context">Password</span><span class="token operator">::</span><span class="token function">min</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">rules</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token keyword">new</span> <span class="token class-name">ZxcvbnRule</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="自定义验证规则" tabindex="-1"><a class="header-anchor" href="#自定义验证规则"><span>自定义验证规则</span></a></h2>
<h3 id="使用规则对象" tabindex="-1"><a class="header-anchor" href="#使用规则对象"><span>使用规则对象</span></a></h3>
<p>Laravel 提供了许多有用的验证规则；但是，你可能希望指定一些自己的规则。注册自定义验证规则的一种方法是使用规则对象。你可以使用 <code v-pre>make:rule</code> Artisan 命令生成新的规则对象。让我们使用这个命令生成一个验证字符串是否大写的规则。Laravel 会将新规则放在 <code v-pre>app/Rules</code> 目录中。如果该目录不存在，当你执行 Artisan 命令来创建规则时，Laravel 将创建它：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:rule Uppercase</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>规则创建后，我们准备定义其行为。规则对象包含一个方法：<code v-pre>validate</code>。此方法接收属性名称、其值和一个回调，失败时应调用该回调以返回验证错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Rules</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Closure</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidationRule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Uppercase</span> <span class="token keyword">implements</span> <span class="token class-name">ValidationRule</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 运行验证规则。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">validate</span><span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token keyword type-hint">mixed</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Closure</span> <span class="token variable">$fail</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">void</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">strtoupper</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token variable">$fail</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'属性必须是大写的'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">        <span class="token punctuation">}</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>定义规则后，你可以通过将规则对象实例与其他验证规则一起传递来将其附加到验证器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Rules<span class="token punctuation">\</span>Uppercase</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">validate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'string'</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Uppercase</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="翻译验证消息" tabindex="-1"><a class="header-anchor" href="#翻译验证消息"><span>翻译验证消息</span></a></h4>
<p>你可以不提供 <code v-pre>$fail</code> 闭包的错误消息，而是提供一个<a href="https://learnku.com/docs/laravel/11.x/localizationmd" target="_blank" rel="noopener noreferrer">翻译字符串键</a>，并指示 Laravel 翻译错误消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">strtoupper</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">!==</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$fail</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'validation.uppercase'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">translate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，你可以为 <code v-pre>translate</code> 方法的第一个和第二个参数提供占位符替代和首选语言：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$fail</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'validation.location'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">translate</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'value'</span> <span class="token operator">=></span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">value</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'fr'</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="访问额外数据" tabindex="-1"><a class="header-anchor" href="#访问额外数据"><span>访问额外数据</span></a></h4>
<p>如果你的自定义验证规则类需要访问正在验证的所有其他数据，你的规则类可以实现 <code v-pre>Illuminate\Contracts\Validation\DataAwareRule</code> 接口。该接口要求你的类定义一个 <code v-pre>setData</code> 方法。在验证进行之前，Laravel 将自动通过所有验证数据调用此方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Rules</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>DataAwareRule</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidationRule</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Uppercase</span> <span class="token keyword">implements</span> <span class="token class-name">DataAwareRule</span><span class="token punctuation">,</span> ValidationRule</span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 正在验证的所有数据。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, mixed></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 设置正在验证的数据。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">array</span></span>&lt;string, mixed>  $data</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">setData</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$data</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">static</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">data</span> <span class="token operator">=</span> <span class="token variable">$data</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>或者，如果你的验证规则需要访问执行验证的验证器实例，则你可以实现 <code v-pre>ValidatorAwareRule</code> 接口：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App<span class="token punctuation">\</span>Rules</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidationRule</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Contracts<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>ValidatorAwareRule</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">Uppercase</span> <span class="token keyword">implements</span> <span class="token class-name">ValidationRule</span><span class="token punctuation">,</span> ValidatorAwareRule</span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 验证器实例。</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@var</span> <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Validation<span class="token punctuation">\</span>Validator</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">protected</span> <span class="token variable">$validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 设置当前验证器。</span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">setValidator</span><span class="token punctuation">(</span><span class="token class-name type-declaration">Validator</span> <span class="token variable">$validator</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword return-type">static</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token variable">$this</span><span class="token operator">-></span><span class="token property">validator</span> <span class="token operator">=</span> <span class="token variable">$validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">        <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="使用闭包" tabindex="-1"><a class="header-anchor" href="#使用闭包"><span>使用闭包</span></a></h3>
<p>如果你的应用程序中只需要自定义规则的功能一次，则可以使用闭包代替规则对象。闭包接收属性的名称、属性的值以及在验证失败时应调用的 <code v-pre>$fail</code> 回调：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Closure</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$request</span><span class="token operator">-></span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'title'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'required'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'max:255'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$attribute</span><span class="token punctuation">,</span> <span class="token keyword type-hint">mixed</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Closure</span> <span class="token variable">$fail</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token variable">$value</span> <span class="token operator">===</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token variable">$fail</span><span class="token punctuation">(</span><span class="token string double-quoted-string">"The <span class="token interpolation"><span class="token punctuation">{</span><span class="token variable">$attribute</span><span class="token punctuation">}</span></span> is invalid."</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span></span>
<span class="line">        <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="隐式规则" tabindex="-1"><a class="header-anchor" href="#隐式规则"><span>隐式规则</span></a></h3>
<p>默认情况下，当验证的属性不存在或包含空字符串时，普通验证规则（包括自定义规则）不会运行。例如，<a href="#rule-unique"><code v-pre>unique</code></a> 规则不会针对空字符串运行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Validator</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$rules</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'unique:users,name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$input</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">''</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Validator</span><span class="token operator">::</span><span class="token function">make</span><span class="token punctuation">(</span><span class="token variable">$input</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">passes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>对于即使在属性为空时也要运行的自定义规则，该规则必须暗示该属性是必需的。要快速生成一个新的隐式规则对象，你可以使用带有 <code v-pre>--implicit</code> 选项的 <code v-pre>make:rule</code> Artisan 命令：</p>
<div class="language-bash line-numbers-mode" data-highlighter="prismjs" data-ext="sh" data-title="sh"><pre v-pre class="language-bash"><code><span class="line">php artisan make:rule Uppercase <span class="token parameter variable">--implicit</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
「隐式」规则仅 暗示 该属性是必需的。实际上，缺少或空属性是否无效取决于你。</p>
</blockquote>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/11.x/validationmd/16668" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/va...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/11.x/validationmd/16668" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/11.x/va...</a></p>
</blockquote>
</div></template>


